【发布时间】:2016-11-13 16:54:52
【问题描述】:
- 我在同一个视图中使用了两次指令。
- 在每个指令中,我调用一个带有字段和 ul 列表的模板。
- 当用户写东西时,我调用 API 返回一个 结果数组。
- 此数组用于通过 ng-repeat (ul) 显示列表。
问题: 如果用户在第一个加载的字段(第一个指令)中写入内容,则调用的 ng-repeat 在第二个指令中。
<div style="padding: 20px;">
<p>First directive :</p>
<span search-box ng-model="toto"></span>
<hr>
<p>Second directive :</p>
<span search-box ng-model="titi"></span>
</div>
myApp.directive('searchBox', [function() {
return {
restrict: 'A',
scope: {
model: '=ngModel',
},
template: ''
+'<div>'
+ '<input type="text" ng-model="model" />'
+ '<ul style="background-color:#e1e1e1; width: 142px; padding: 15px;" ng-show="cities.length">'
+' <li ng-repeat="city in cities">'
+' {{city.label}}'
+' </li>'
+ '</ul>'
+'</div>',
replace: true,
transclude: true,
link: function(scope, element, attrs, ngModel) {
scope.cities = [];
scope.$watch('model', function (newValue, oldValue) { if(newValue != oldValue && newValue.length > 0) search(newValue) });
search = function(input) {
scope.cities = [
{label: 'Paris'},
{label: 'London'},
{label: 'New York'},
{label: 'Berlin'},
{label: 'Lisbonne'}
];
};
}
}
请在第一个字段中写一些内容,结果框显示在第二个字段下方。为什么 ul 不引用自己的指令?
【问题讨论】:
标签: angularjs directive angular-ngmodel