javascript - AngularJs如何将数据从父组件发送到子组件

标签 javascript angularjs

这似乎是一个非常简单的问题,但我已经有一段时间没有得到它的答案了。问题是,当你有一个组件层次结构时,你如何将数据从父组件传递到子组件,以便子组件能够在它们的范围内(或在其他变量中?)访问该数据。

我正在使用 AngularJs 1.5.5

这是我现在拥有的东西,我在 JavaScript 代码中添加了关于我实际想要实现的注释。 - https://plnkr.co/edit/blY85rvARIqkmfCnRBOV?p=preview

index.html

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <div ng-app="myApp">
      <dependency></dependency>
    </div>
  </body>

</html>

脚本.js

// Code goes here
angular.module('myApp', ['dependency']);

// this is parent component
angular.
module('dependency', ['dependency2']).
component('dependency', {
  templateUrl: 'dependency.html',
  controller: ['$scope', function dependencyController($scope) {
    $scope.dependencyScopedVariable = "Some local variables of dependency";
    $scope.childComponentParams = [{ name: "child1"}, { name: "child2"}];
  }]
});

// this is child component
angular.
module('dependency2', []).
component('dependency2', {
  templateUrl: 'dependency2.html',
  controller: ['$scope', function dependency2Controller($scope) {
    // How to access childParam from parent here?
    $scope.itemGotFromParent = "this should be from parent";
  }]
});

依赖.html

<div>{{dependencyScopedVariable}}</div>
<dependency2 ng-repeat="childParam in childComponentParams"></dependency2>

依赖2.html

<!-- How to get the childParam repeated in ng-repeat here? -->
<div>{{itemGotFromParent}}</div>

最佳答案

当您使用组件时,它本身就是指令的糖语法。
你可以
1) require 父组件的 Controller 。有关更多信息,请阅读 intercomponent communication 2) 将数据作为 bindings 传递。

看下面的例子:

示例#1:

// Code goes here
angular.module('myApp', ['dependency']);

// this is parent component
angular
  .module('dependency', ['dependency2'])
  .component('dependency', {
    template: '<div>{{$ctrl.dependencyScopedVariable}}</div>' +
      '<dependency2 ng-repeat="childParam in ' + '$ctrl.childComponentParams"></dependency2>',
    controller: function dependencyController() {
      this.dependencyScopedVariable = "Some local variables of dependency";
      this.childComponentParams = [{
        name: "child1"
      }, {
        name: "child2"
      }];
    }
  });

// this is child component
angular.
module('dependency2', [])
  .component('dependency2', {
    require: {
      dependencyCtrl: '^dependency'
    },
    template: '<div>{{$ctrl.itemGotFromParent}}</div>',
    controller: function dependency2Controller() {
      
      this.$onInit = function() {
        this.itemGotFromParent = this.dependencyCtrl.childComponentParams;
      }
    }
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
<script src="script.js"></script>

<div ng-app="myApp">
  <dependency></dependency>
</div>

示例#2:使用组件的绑定(bind)(指令范围)

// Code goes here
angular.module('myApp', ['dependency']);

// this is parent component
angular
  .module('dependency', ['dependency2'])
  .component('dependency', {
    template: '<div>{{$ctrl.dependencyScopedVariable}}</div>' +
      '<dependency2 data="childParam" ng-repeat="childParam in ' + '$ctrl.childComponentParams"></dependency2>',
    controller: function dependencyController() {
      this.dependencyScopedVariable = "Some local variables of dependency";
      this.childComponentParams = [{
        name: "child1"
      }, {
        name: "child2"
      }];
    }
  });

// this is child component
angular.
module('dependency2', [])
  .component('dependency2', {
    bindings: {
      data: '<'
    },
    template: '<div>{{$ctrl.itemGotFromParent}}</div>',
    controller: function dependency2Controller() {

      this.$onInit = function() {
        this.itemGotFromParent = this.data;
      }
    }
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
<script src="script.js"></script>

<div ng-app="myApp">
  <dependency></dependency>
</div>

关于javascript - AngularJs如何将数据从父组件发送到子组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42608083/

相关文章:

javascript - angularJS 过滤日期

带有返回代码和返回消息的 PHP 函数,仅供引用

javascript - 以模式显示搜索栏查询结果

angularjs - 如何使用 Angular JS 隐藏地址栏中显示的 stateProvider url?

javascript - AngularJs 在初始化之前加载所有文件

javascript - ng :disabled does not work with select html element

javascript - 我的 jquery 验证添加方法密码规则出现问题

javascript - Backbonejs 集合获取错误检测 XHR 对象

javascript - 删除动态列的值的总和计算错误

django - 即使在登录后,Tastypie 请求也显示匿名用户