【发布时间】:2019-08-13 23:38:07
【问题描述】:
我看过之前的答案,但我不确定它们是否是我需要的答案。
我有一个指令,我们称之为“selectValue”。一个值可以有一个默认值,我们称之为“$scope.default”。
指令在一个地方看起来像这样:
<select-value ng-model="data.input" controlId="inputSelector" />
但它会在另一个看起来像这样:
<select-value ng-model="myValue" controlId="inputSelector" />
我没有使 ng-model 输入相同的选项;它们在不同的地方使用,这是一个遗留代码库。
这是指令的定义:
.directive('selectValue', ['$timeout', function($timeout) {
const directive = {
restrict: 'E',
scope: {
controlId: '@',
model: '=?'
},
controller: 'selectValueCtrl',
template: '<input id="{{controlId}}" name="{{controlId}}" placeholder="Enter Value" type="text" ng-model="model" />'
};
return directive;
}
问题:我该怎么做才能在<select-value> 的“模型”属性中输入不同的输入以使其访问不同的范围变量?
编辑:引用的“重复”问题是指为 ng-click 设置值,而不是在表单控件中引用 ng-model。
【问题讨论】:
-
你试过
model: '@'在指令中绑定作用域吗? -
应该避免在元素指令中使用自闭合语法。它会导致 HTML5 出现意外行为。始终包含结束标记
</select-value>。
标签: angularjs