[英]Pass argument to directive in angularjs?
我想將參數傳遞給angularjs中的指令。
我在stackoverflow Angularjs上找到了一些線程-將參數傳遞給指令
但這對我沒有幫助。
指示:
app.directive('datePicker', function () {
return {
restrict: 'E',
replace: true,
template: '<input type="text" class="form-control" ng-model="modelValue">',
scope: {
modelValue: '=',
format: '@',
},
link: function (scope, element, form) {
$(element).datepicker({
dateFormat: format,
});
}
}
})
元件:
<date-picker model-value="salary.month" format='MM-YYYY'></date-picker>
在這里,我想使用format
作為傳遞directive
屬性,因此我可以將相同的date-picker
指令使用不同的格式。
我嘗試使用上面的代碼示例,模型值有效,但是格式無效。
請幫我找到解決方案
您應該使用scope.format
來檢索format屬性的值
link: function (scope, element, form) {
$(element).datepicker({
dateFormat: scope.format,
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.