简体   繁体   English

如何在AngularJS中将数据从父级发送到子级

[英]How to send data from the Parent to the Child in the AngularJS

How can I send the data from the Parent Controller (index) to the Child Controller (Component). 如何将数据从父控制器 (索引)发送到子控制器 (组件)。 Please see the example, instead of the hardcoding the url I want to send the parameters for the Title and the URL from the page controller to the component. 请参见示例,而不是对url进行硬编码,我想将TitleURL的参数从页面控制器发送到组件。 I'm stuck to send it and I don't know where is the problem. 我一直无法发送,不知道问题出在哪里。

index.html 的index.html

<div>
  <my-list obj="vm.obj"></my-list>
</div>

index.controller.js index.controller.js

this.obj = {
    testURL: "AngularJS",
    testName: "Testing Environment
}

mylistComponent.html mylistComponent.html

<span>{{vm.myTestName}}</span>
<a href="https://en.wikipedia.org/wiki/{{vm.myTestURL}}">AngularJS Wikipedia</a>

mylist.Component.js mylist.Component.js

binding: {
    obj: "="
}

this.goToPage = function() {
    this.myTestName = this.obj.testName;
    this.myTestURL = this.obj.testURL;
}

PLUNKER PLUNKER

In your plunker, by doing this : 通过这样做,您可以:

<test-component value="vm.obj"> </test-component>

You put the value of obj in the var value . 您将obj的值放在var value So in the child scope you can access to this value with the var value not anymore with obj . 因此,在子作用域中,可以使用obj不再使用var value来访问此值。

Here is your plunker updated. 是您的朋克更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM