传递NG-模型嵌套指令 [英] Passing ng-model in nested directives

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

问题描述

我想从外的指令我的纳克模型传递给一个内diretive'(其包含在外层指令模板)。

什么是正确的方式做呢?

HTML code:

 <身体GT;
    <外指令NG模型=托/>
< /身体GT;

和指令code:

  angular.module(应用,[])。指令(outerDirective',函数(){
    返回{
        模板:'<内指令NG模型=托/>,
        链接:功能(){...}
    }
});


解决方案

您可以建立一个双向绑定(见的文档,部分指令定义对象)在 ngModel 属性变量,与任何其他指令:

<我的指示性NG模型=foo的>< /我-指令>

myApp.directive('myDirective',函数(){
    返回{
        模板:'< D​​IV><输入类型=文本NG模型=ngModel/>< / DIV>,
        更换:真实,
        范围: {
            ngModel:'=',
        },
    };
});

<大骨节病> 小提琴

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

What is the correct way for doing it?

HTML code:

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

and directive code:

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

解决方案

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

Fiddle

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

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