我想将参数传递给 angularjs 中的指令。
我在 stackoverflow 上发现了一些线程 Angularjs - Pass argument to directive
但这对我没有帮助。
指令:
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
作为属性来传递指令
,所以我可以使用相同的date-picker
指令但格式不同。
我已尝试使用上面的代码示例,模型值有效,但格式无效。
请帮我找到解决办法
最佳答案
您应该使用scope.format
来检索格式属性的值
link: function (scope, element, form) {
$(element).datepicker({
dateFormat: scope.format,
});
}
关于javascript - 将参数传递给 angularjs 中的指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32074497/