在定义指令的scope属性如果设置成了{},那就成为了一个独立作用域,如果要传入一个方法,使用&,但是这里的传参有点不一样。
先看下官网解释:
& or &attr - provides a way to execute an in the context of the parent scope. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given and widget definition of scope: { localFn:'&myAttr' }, then isolate scope property localFn will point to a function wrapper for the count = count + value . Often it's desirable to pass data from the isolated scope via an and to the parent scope, this can be done by passing a map of local variable names and values into the wrapper fn. For example, if the is increment(amount) then we can specify the amount value by calling the localFn as localFn({amount: 22}).
这里有个例子:
<!DOCTYPE html>
<html>
<head lang="en">
< charset="UTF-8">
< ></ >
</head>
<body ng-app="app1">
<div ng-controller="MyCtrl">
<div ng-repeat="item in items" my-component isolated- -foo="updateItem(item,temp)">
{{item|json}}
</div>
</div>
</body>
< src="../ s/angular.js"></ >
< >
var myModule = angular.module('app1', [])
.directive('myComponent', function () {
return {
restrict:'A',
scope:{
isolated Foo:'&'
},
:function(scope,element,attr) {
scope.isolated Foo();
}
};
})
.controller('MyCtrl', ['$scope', function ($scope) {
$scope.items=[{id:1,value:"test"},{id:2,value:"TEst2"}];
$scope.updateItem = function (item,temp) {
console.log("Item param "+item.id);
console.log("temp param " + temp);
}
}]);
</ >
</html>
以上这篇AngularJs1.x自定义指令独立作用域的函数传入参数方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
-
711便利店:日本计划关店1000家,母公司将裁员3000人
2026-05-18栏目: 教程
-
阿里云服务器共享型实例xn4 n4 性价比高适合个人博客 中小型网站
2026-05-18栏目: 教程
-
QQ上这3个隐藏功能,“偷偷”告诉谁在暗恋你,不准不要你钱
2026-05-18栏目: 教程
-
戴森电动汽车梦破碎 业内人士:及时止损明智之举
2026-05-18栏目: 教程
-
SpaceX提交3万颗Starlink宽带星座卫星发射申请
2026-05-18栏目: 教程
