简体   繁体   English

AngularJS-在自定义指令中继承ngModel

[英]AngularJS - Inherit ngModel in custom directive

I have a custom directive like this: 我有一个这样的自定义指令:

myText.html: myText.html:

<div>
<label>{{label}}</label>
<input type="text" class="form-control" >
</div>

Javascript: 使用Javascript:

app.directive("myText", function() {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: "shared/form/myText.html",
        scope : {
            label : "@",
        }
    };
});

I just want to wrap an input and handle a label as an attribute. 我只想包装输入并将标签作为属性来处理。

In my view i use the directive this way: 在我看来,我以这种方式使用指令:

<my-text label="Name" ng-model="person.firstname"></my-text>

The problem is that ng-model is not binding the model to my input. 问题在于ng-model没有将模型绑定到我的输入。

What is the correct way to achieve this result? 实现此结果的正确方法是什么? Thanks 谢谢

Put ng-model on the input and bind it to the isolate scope. ng-model放在输入上,并将其绑定到隔离范围。

<my-text label=Name model=person.firstname></my-text>

<input type="text" class="form-control" ng-model=model>

return {
    restrict: 'E',
    replace: true,
    templateUrl: "shared/form/myText.html",
    scope : {
        label : "@",
        model: "=",
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM