AngularJs1.x自定义指令独立作用域的函数传入参数方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了AngularJs1.x自定义指令独立作用域的函数传入参数方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1734字,纯文字阅读大概需要3分钟。
内容图文
在定义指令的scope属性如果设置成了{},那就成为了一个独立作用域,如果要传入一个方法,使用&,但是这里的传参有点不一样。
先看下官网解释:
& or &attr - provides a way to execute an expression 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 expression. Often it's desirable to pass data from the isolated scope via an expression and to the parent scope, this can be done by passing a map of local variable names and values into the expression wrapper fn. For example, if the expression is increment(amount) then we can specify the amount value by calling the localFn as localFn({amount: 22}).
这里有个例子:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body ng-app="app1"> <div ng-controller="MyCtrl"> <div ng-repeat="item in items" my-component isolated-expression-foo="updateItem(item,temp)"> {{item|json}} </div> </div> </body> <script src="../scripts/angular.js"></script> <script> var myModule = angular.module('app1', []) .directive('myComponent', function () { return { restrict:'A', scope:{ isolatedExpressionFoo:'&' }, link:function(scope,element,attr) { scope.isolatedExpressionFoo(); } }; }) .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); } }]); </script> </html>
以上这篇AngularJs1.x自定义指令独立作用域的函数传入参数方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
内容总结
以上是互联网集市为您收集整理的AngularJs1.x自定义指令独立作用域的函数传入参数方法全部内容,希望文章能够帮你解决AngularJs1.x自定义指令独立作用域的函数传入参数方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。