【发布时间】:2017-03-08 16:28:25
【问题描述】:
我定义了三个指令:
-
家长
这应该在其他两个指令之间共享变量 - 孩子一和孩子二。
-
儿童一号
这包含一个代表搜索词的输入字段。每当这种情况发生变化时,我都会使用链接函数来更新存储在父控制器中的变量。
在实际使用中,我将根据该术语进行搜索并更新数组。但是为了简单起见,我只想创建一个长度为 1 的新数组,我想用搜索词作为它的值来填充它。
-
孩子二
这应该会显示结果数组。
由于某种原因,这不起作用,如果我将数组的长度设置为 0 并推送该值,我将在 Child Two 中看到视图更新(我已经注释掉了实现这一点的代码),但我想了解为什么设置数组值不起作用。
我知道一个服务在这里很合适,但是这个指令可能会在同一个页面上重复使用多次,所以我不希望页面上每个项目之间的范围发生冲突。
为什么视图没有用我当前使用的代码更新?
var app = angular
.module('SampleApplication', [])
.directive('parent', function() {
return {
restrict: 'E',
transclude: true,
template: "<div ng-transclude></div>",
controller: function($scope) {
this.searchTerm = "";
this.arrayContainingSearchTerm = [{value: ''}];
this.updateSearchTerm = function(searchTerm) {
this.searchTerm = searchTerm;
//When this array is assigned - it doesn't get updated in the view
this.arrayContainingSearchTerm = [{value: searchTerm}];
//This will update the view.
//this.arrayContainingSearchTerm.length = 0;
//this.arrayContainingSearchTerm.push([{value: searchTerm}]);
};
}
}
})
.directive('childOne', function() {
return {
restrict: 'E',
require: '^^parent',
template: "<div><h1>Child One</h1><input ng-model='searchTerm'></input></div>",
link: function(scope, element, attrs, parentController) {
scope.$watch('searchTerm', function(newValue, oldValue) {
parentController.updateSearchTerm(newValue);
});
}
}
})
.directive('childTwo', function() {
return {
restrict: 'E',
require: '^^parent',
template: "<div><h1>Child Two</h1><h2>Value below should be: {{searchTerm}}</h2><h2>{{arrayContainingSearchTerm}}</h2></div>",
link: function(scope, element, attrs, parentController) {
scope.searchTerm = parentController.searchTerm;
scope.arrayContainingSearchTerm = parentController.arrayContainingSearchTerm;
}
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<div ng-app="SampleApplication">
<parent>
<child-one></child-one>
<child-two></child-two>
</parent>
</div>
【问题讨论】:
标签: javascript angularjs angularjs-directive angularjs-scope