我试图创建一个自定义电子邮件文本框组件,它有两个字段。要做到这一点,我有这个模板:AngularJS:指令绑定
<div class="custom">
<div class="username" contenteditable="true"></div>
<div class="domain">@{{ domainValue }}</div>
</div>
所以我可以调用指令调用该模板,如:
<div ng-custom-txt></div>
从我希望能够通过不同类型的值的指令(从模型域)被称为“NG-domaindata”,如:
<div ng-domaindata="mydomain1.com" ng-custom-txt></div>
我的问题是,我该怎么绑定“域”字段是在模板中的指令?
我试图用这种方法,但没有成功可言:
模板:customtemplate.html
<div class="custom">
<div class="username" contenteditable="true"></div>
<div class="domain">@{{ domainValue }}</div>
</div>
页
<div ng-domaindata="mydomain1.com" ng-custom-txt></div>
<div ng-domaindata="mydomain2.com" ng-custom-txt></div>
指令
app.directive('ngCustomTxt', function() {
return {
restrict: 'A',
require: '^ngModel',
templateUrl: 'customtemplate.html',
link: function(scope, element, attrs, ctrl) {
scope.$watch(attrs.ngDomaindata, function (newValue){
scope.domainValue = newValue;
}
}
}
});
显然它不起作用,因为我无法区分这两个元素,有人可以帮我解决这个问题吗?
只是一个快速提示。避免使用带ng前缀的自己的指令。 –