我有一个包含元素名称的输入字符串(我的意思是“名称”属性)。如何在 $scope 中搜索具有给定名称的元素?
我想做的是制定一个参数化指令
<select bindslave="shipping_state" name="billing_state" ng-model="order.billing_state" ng-options="i.name for i in billingRegions" value="{{ order.billing_state.id }}"></select>
<select name="shipping_state" ng-model="order.shipping_state" ng-options="i.name for i in shippingRegions" value="{{ order.shipping_state.id }}"></select>
目前,CoffeeScript 中的代码如下所示:
app_directives.directive "bindslave", ->
require: "ngModel"
link: (scope, element, attrs, ctrl) ->
ctrl.$parsers.unshift (viewValue) ->
if scope.sameAsBilling
switch attrs['bindslave']
when "shipping_state"
scope.order.shipping_state = viewValue
when "shipping_country"
scope.order.shipping_country = viewValue
viewValue
我想摆脱 switch 语句并搜索具有 name==attrs['bindslave'] 的元素