我有一个包含元素名称的输入字符串(我的意思是“name”属性)。 如何搜索具有给定名称的元素的$ scope?Angular.js - 元素的搜索范围
我想要做的就是让一个参数化指令
<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语句并搜索元素与名称== attrs ['bindslave']
我更新的问题。如果你的回答仍然适用于此,请告诉我。 – Paul