我對 Angular 很陌生。我正在嘗試從模板讀取/傳遞一些資料到我的角度指令。
<div class="col-md-6" approver-picker="partner.approverPlan.data" data-pickerType="PLAN"></div>
我在我的角度模板中有這個,我在不同的地方有這個。所以我想在我的 Angular 代碼中知道點擊了哪個選擇器。
我正在考慮讀取data-pickerType
指令中的值。(我可以在 jQuery 中讀取此值,但不知道如何在 Angular 中讀取)
這是我的指令代碼。
Partner.Editor.App.directive('approverPicker', [
'$compile', function ($compile) {
return {
scope: {
"approvers": "=approverPicker"
},
templateUrl: '/template/assets/directive/ApproverPicker.html',
restrict: 'EA',
link: function ($scope, element) {
...........
}
};
}
]);
如何讀取data-pickerType
指令中的值,或者有更好的方法嗎?
uj5u.com熱心網友回復:
attrs
通過在鏈接函式中傳遞變數,可以在 Angular 指令中訪問資料屬性。
Partner.Editor.App.directive('approverPicker', [
'$compile', function ($compile) {
return {
scope: {
"approvers": "=approverPicker"
},
templateUrl: '/template/assets/directive/ApproverPicker.html',
restrict: 'EA',
link: function ($scope, element, attrs) { //passing attrs variable to the function
//accessing the data values
console.log(attrs.pickertype);
//you can access any html attribute value for the element
console.log(attrs.class);
}
};
}
]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/507807.html