在嵌套指令中传递 ng-model [英] Passing ng-model in nested directives

查看:36
本文介绍了在嵌套指令中传递 ng-model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的 ng-model 从外部指令"传递到内部指令"(包含在外部指令模板中).

I want to pass my ng-model from the 'outer-directive' to an 'inner-diretive' (which is contained in the outer-directive template).

正确的做法是什么?

HTML 代码:

<body>
    <outer-directive ng-model="prop" />
</body>

和指令代码:

angular.module('app', []).directive('outerDirective', function(){
    return {
        template: '<inner-directive ng-model="prop" />',
        link: function() { ... }
    }
});

推荐答案

您可以设置双向绑定(请参阅 documentation, section "Directive Definition Object") 与 ngModel 属性中的变量,与任何其他指令一样:

You can set up a bi-directional binding (see the documentation, section "Directive Definition Object") with the variable in ngModel attribute, as with any other directives:

<my-directive ng-model="foo"></my-directive>

myApp.directive('myDirective', function () {
    return {
        template: '<div><input type="text" ng-model="ngModel" /></div>',
        replace: true,
        scope: {
            ngModel : '=',
        },
    };
});

小提琴

这篇关于在嵌套指令中传递 ng-model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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