javascript - 将参数传递给 angularjs 中的指令?

标签 javascript jquery angularjs angularjs-directive

我想将参数传递给 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/

相关文章:

javascript - Backbone.js 嵌套对象结构

javascript - 如何使用 jQuery 在客户端的 HTML 表中显示图像

angularjs - 在 ag-Grid 中选择分组行

JavaScript、Jquery : get element by two attr?

javascript - 将 prop 传递到自定义组件中

javascript - 想要在选择多个中添加无作为值

javascript - 为数组中的每个对象创建具有条件的 div

javascript - AngularJS 变量

javascript - 内联 SVG 与 CSS 背景 Sprite

javascript - catch block 中的 Redux-saga 产量 put() 未执行