0
我有一个标记:我为什么必须经过两次 - 隔离范围的方法参数?
<div ng-controller="ProductController">
<inventory-Product
on-report="showData(p.name,p.price)"
ng-repeat="p in products"
name="{{p.name}}"
price="{{p.price}}">
</inventory-Product>
</div>
而且一个指令:
myApp.directive("inventoryProduct", function()
{
return {
restrict: "E",
scope : {
name : "@",
price: "@",
onReport:"&"
},
template: '<div>{{name}} costs {{ price}}$ </div> \
<div>\
<button \
class="btn btn-lg btn-danger" \
ng-click="onReport({name:name,price:price})">\
Change name\
</button>\
</div>'
}
})
(代码是工作)
问:
的onReport:"&"
允许指令调用一些parentScope函数并传入其参数。
好了 - 所以onReport
就像委托指向任何功能,在HTML标记被宣布:
on-report="showData(p.name,p.price)"
但如果该指令的模板已经发送论点:
ng-click="onReport({name:name,price:price})"
而且parentScope的控制器功能已经知道功能:
$scope.showData = function (name,price)
{
alert("name="+name+" price="+price)
}
然后为什么我必须提及
on-report="showData(p.name,p.price)" ?
换句话说再次参数,似乎更符合逻辑写
on-report="showData"
所以它只是点的功能,然后用这个路径去:
我缺少什么?
谢谢。当你说_调用控制器不能访问这些值,因为它们不在其范围内 - - 什么调用控制器?换句话说,我的样本中实际发生了什么? (参数POV),如果控制器没有这些值会怎么样? –
调用控制器是ProductController。它在由该控制器控制的视图内部,该指令被使用。所以showData()是一个暴露在该控制器范围内的函数。 –
*如果控制器没有这些值,那该怎么办?然后你会使用'on-report =“showData(name,price)”',这两个参数将由指令提供,使用'onReport({name:name ,价格:价格})'。 –