将参数传递给angularjs中的指令? [英] Pass argument to directive in angularjs?

查看:110
本文介绍了将参数传递给angularjs中的指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将参数传递给angularjs中的指令.

I want to pass argument to directive in angularjs.

我在stackoverflow上找到了一些线程 Angularjs-将参数传递给指令

I found some thread on 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作为传递directive的属性,因此我可以使用相同的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.

请帮助我找到解决方法

推荐答案

您应使用scope.format检索格式属性的值

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

link: function (scope, element, form) {
    $(element).datepicker({
        dateFormat: scope.format,
    });
}

这篇关于将参数传递给angularjs中的指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆