angularJS自定義directive之帶參方法傳遞詳解
更新時間:2018年10月09日 09:18:54 作者:螞蟻沖鋒隊
今天小編就為大家分享一篇angularJS自定義directive之帶參方法傳遞詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
//自定義指令 "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調用 <my-email to-dir="廣東中山" from-name="海南??? send-email="send(msg)"/>
功能:點擊【提交】后,將自定義指令myEmail中textarea元素的內容傳遞給控制器中的send()方法。
關鍵點:模板email.html中的ng-click="sendEmail({msg:content})" 參數(shù){msg:content}必須是一個鍵值對,鍵為:方法參數(shù)名 值為:傳遞的內容
以上這篇angularJS自定義directive之帶參方法傳遞詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
AngularJS中$watch和$timeout的使用示例
這篇文章給大家介紹了AngularJS中$watch和$timeout的使用例子,通過示例代碼相信更能讓大家理解,有需要的朋友們下面來一起看看吧。2016-09-09angularjs過濾器--filter與ng-repeat配合有奇效
本篇文章主要介紹了angularjs過濾器-filter與ng-repeat的相關知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04Angular實現(xiàn)類似博客評論的遞歸顯示及獲取回復評論的數(shù)據(jù)
這篇文章主要給大家介紹了關于Angular如何實現(xiàn)類似博客評論的遞歸顯示及獲取回復評論的數(shù)據(jù)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-11-11