我有这样一个组成部分:AngularJS +1.5哪有父控制器将数据传递到组件控制器
Check Plunkr Example(更新,并与解决方案合作:)谢谢)
我-component.js声明
(function() {
'use strict';
angular
.module('app')
.component("myComponent", {
bindings: { obj: "=" },
controller: "ComponentController",
controllerAs: "vm",
template:
'<section class="result"><h2>{{vm.title}}</2>' +
'<h3>Using the Parent Controller values</h3>' +
'<ul><li>{{ vm.obj.a }}</li><li>{{vm.obj.b}}</li></ul>' +
'<h3>Using the Children controller values:'+
'<ul class="component-controller"><li>{{ vm.copiedObj.a }}</li><li>{{vm.copiedObj.b}}</li></ul>' +
'</section>'
});
})();
我的组件控制器
(function() {
'use strict';
angular
.module('app')
.controller('ComponentController', ComponentController);
function ComponentController() {
var vm = this;
vm.title = "I'm the Children controller"
vm.copiedObj = vm.obj; // This should store the myObj variable located in the MainController
}
})();
和一个父控制器
(function() {
'use strict';
angular
.module('app', []);
})();
// app.controller.js
// ///////////////////
(function() {
'use strict';
angular
.module('app')
.controller('MainController', MainController);
function MainController() {
var vm = this;
vm.title = "I'm the Parent controller";
vm.myObj = {
a: "I'm the first value",
b: "I'm the second value"
};
}
})();
所以,如果我有一个像
<body ng-controller="MainController as vm">
<h2>{{vm.title}}</h2>
<my-component obj="vm.myObj"></my-component>
</body>
重要的线,模板是在那里我的obj = “vm.myObj” 吧? 我有什么错的,因为甚至没有采取vm.title:S
我不想只是打印vm.myObj值到组件, 我想从ParentController的vm.obj.value存储在ComponentController中的可访问&。
' my-component>'应该可以工作.... –
Claies
但它不会... –
好吧,您的示例实际上并未显示您*使用你传入的值,所以你的具体问题可能不是很明显。 – Claies