angularJS自定義directive之帶參方法傳遞



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})" 參數{msg:content}必須是一個鍵值對,鍵爲:方法參數名    值爲:傳遞的內容


一起探討、一同進步!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章