Pass argument to directive in angularjs?

2019-09-08 04:08发布

I want to pass argument to directive in angularjs.

I found some thread on stackoverflow Angularjs - Pass argument to directive

But it is not helped me.

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,
            });
        }
    }
})

Element:

<date-picker model-value="salary.month" format='MM-YYYY'></date-picker>

Here I want to use format as attribute to pass directive, So I can use same date-picker directive with different format.

I have tried with above code example,model value is working but format is not working.

Please help me to find solution

1条回答
干净又极端
2楼-- · 2019-09-08 05:07

You should use scope.format to retrieve the value of the format attribute

link: function (scope, element, form) {
    $(element).datepicker({
        dateFormat: scope.format,
    });
}
查看更多
登录 后发表回答