在定義指令的scope屬性如果設(shè)置成了{(lán)},那就成為了一個獨立作用域,如果要傳入一個方法,使用&,但是這里的傳參有點不一樣。
創(chuàng)新互聯(lián)建站堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計制作、成都網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的丹棱網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
先看下官網(wǎng)解釋:
& 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自定義指令獨立作用域的函數(shù)傳入?yún)?shù)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前題目:AngularJs1.x自定義指令獨立作用域的函數(shù)傳入?yún)?shù)方法
網(wǎng)站網(wǎng)址:http://chinadenli.net/article0/ppcjoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、云服務(wù)器、域名注冊、虛擬主機、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)