我有這樣一個組成部分: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