如下所示:
//自定義指令 "myEmail"grgApp.directive("myEmail",function(){return{restrict:'AE',scope:{toDir:'@', fromName:'@', sendEmail:'&'},templateUrl:'/htmls/main/html/custom/email.html',}});//控制器中的方法$scope.send=function(msg){ alert("send email! msg: "+msg); } //email.html<div style="width: 100%;height: 100%;color: white;font-size: 0.8rem;"><label style="width: 100%;height: 15%;" ng-bind="toDir"></label><label style="width: 100%;height: 15%;" ng-bind="fromName"></label><textarea style="width: 100%;height: 25%;color: black;" ng-model="content"></textarea><button style="width: 10%;height: 15%;color: black;" ng-click="sendEmail({msg:content})">提交</button></div>//html調(diào)用<my-email to-dir="廣東中山" from-name="海南海口" send-email="send(msg)"/>
功能:點(diǎn)擊【提交】后,將自定義指令myEmail中textarea元素的內(nèi)容傳遞給控制器中的send()方法。
關(guān)鍵點(diǎn):模板e(cuò)mail.html中的ng-click="sendEmail({msg:content})" 參數(shù){msg:content}必須是一個(gè)鍵值對(duì),鍵為:方法參數(shù)名 值為:傳遞的內(nèi)容
以上這篇angularJS自定義directive之帶參方法傳遞詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注