- 同じビューで指示を2回使用しています。
- 各ディレクティブでは、フィールドとulリストを持つテンプレートを呼び出します。
- ユーザーが何かを書くとき、私は の結果の配列を返すAPIを呼び出します。
- この配列は、ng-repeat(ul)でリストを表示するために使用されます。
問題: を最初にロードされるフィールド(最初のディレクティブ)で何かを書くユーザーは、呼び出されたNG-repeatが第二のディレクティブにある場合。ngModel参照の指示を複数回呼び出すビュー
<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'}
];
};
}
}
、最初のフィールドに何かを書く、結果ボックスは、2番目のフィールドの下に表示されます。なぜulはそれ自身の指令を参照していないのですか?
、先端のための感謝! :) –