gpt4 book ai didi

javascript - 在子指令之间共享数据

转载 作者:行者123 更新时间:2023-12-01 03:55:53 27 4
gpt4 key购买 nike

我定义了三个指令:

  • 父级

    这应该在其他两个指令之间共享变量 - Child One 和 Child Two。

  • child 一

    这包含一个表示搜索词的输入字段。每当这种情况发生变化时,我都会使用链接函数来更新存储在父 Controller 中的变量。

    在实际使用中,我将根据该术语进行搜索并更新数组。但为了简单起见,在本例中,我只想创建一个长度为 1 的新数组,并用搜索词的值填充该数组。

  • child 二

    这应该显示结果数组。

由于某种原因,这不起作用,如果我将数组的长度设置为 0 并推送该值,我将在子二中看到 View 更新(我已注释掉实现此目的的代码),但我想了解为什么设置数组值不起作用。

我知道服务适合这里,但该指令可能会在同一页面上多次重复使用,因此我不希望页面上每个项目之间的范围发生冲突。

为什么 View 没有用我当前使用的代码更新?

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>

最佳答案

子作用域自动继承父作用域,并且可以使用 $parent/需要父 Controller 专门访问父作用域,但请注意,同级作用域无法[轻松]访问彼此的作用域。因此,解决方案是更新父作用域并将父作用域更改反射(reflect)回目标子作用域。

您不需要更新子作用域,因为子作用域会自动从父作用域继承。另外,无需查看 searchTerm ngModel,只需在 childOne 指令中使用 attrs.ngModel 即可。

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) {
// Just use attrs.ngModel
parentController.updateSearchTerm(attrs.ngModel);
}
}
})
.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) {
// Comment/remove this since the scope is automatically inherit from parent scope
//scope.arrayContainingSearchTerm = parentController.arrayContainingSearchTerm;
//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 - 在子指令之间共享数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42676713/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com