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

标签 javascript angularjs angularjs-directive angularjs-scope

我定义了三个指令:

  • 父级

    这应该在其他两个指令之间共享变量 - 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/

相关文章:

javascript - $http post 调用 OPTIONS 未发布?

angularjs - 引用错误 : spyOn is not defined Angularjs/Karma/Jasmine

javascript - AngularJS:独立于 $routeProvider 的 Controller 出现 ReferenceError

angularjs - 如何在使用 ng-idle 时限制浏览器选项卡消息

javascript - 在 String.prototype.replace 使用的字符串中转义美元符号的更好方法

javascript - 关于html输入标签

javascript - Angular + ngClass 未应用于 ngView 之外

c# - 如何检测应用程序是在 Windows RT 还是 Windows 8 Pro 上运行?

javascript - Angular : How can I avoid tightly coupling stacked directives when both need values from parent scope?

javascript - AngularJS:以编程方式将 ngIf 添加到指令的最佳实践是什么?