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調(diào)用
<my-email to-dir="廣東中山" from-name="海南海口" send-email="send(msg)"/>
功能:點擊【提交】后,將自定義指令myEmail中textarea元素的內(nèi)容傳遞給控制器中的send()方法。
關(guān)鍵點:模板email.html中的ng-click="sendEmail({msg:content})" 參數(shù){msg:content}必須是一個鍵值對,鍵為:方法參數(shù)名 值為:傳遞的內(nèi)容
以上這篇angularJS自定義directive之帶參方法傳遞詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Angularjs過濾器實現(xiàn)動態(tài)搜索與排序功能示例
這篇文章主要介紹了Angularjs過濾器實現(xiàn)動態(tài)搜索與排序功能,涉及AngularJS過濾器相關(guān)搜索、查詢、排序操作技巧,需要的朋友可以參考下2017-12-12
Angular將填入表單的數(shù)據(jù)渲染到表格的方法
這篇文章主要介紹了Angular將填入表單的數(shù)據(jù)渲染到表格的方法,非常具有實用價值,需要的朋友可以參考下2017-09-09
Angularjs中ng-repeat-start與ng-repeat-end的用法實例介紹
這篇文章主要給大家介紹了Angularjs中ng-repeat-start與ng-repeat-end的用法,文章開始先進行了簡單的介紹,而后通過完整的實例代碼詳細給大家介紹這兩者的用法,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12
Webpack 實現(xiàn) AngularJS 的延遲加載
這篇文章主要介紹了Webpack 實現(xiàn) AngularJS 的延遲加載的相關(guān)資料,需要的朋友可以參考下2016-03-03

