我试图从父组分在角1.5Angularjs 1.5嵌套组件绑定
值可以从父母被更新过的值他嵌套孩子组分,但孩子不能编辑它,只显示它。所以是一个单向绑定'<'对不对?
而且我无法在父组件声明中传递子组件,因为父组件也会有其他用途。
重点是我的父母组件有共同的数据存储,但他们 儿童会以不同的方式使用它。
而父母组件会被多次使用,与不同的 孩子,这就是为什么我不能通过父母 宣言中的孩子。我需要绑定的信息,自动更新的目的,当 家长更新数据,必须由孩子
HTML
<parent-component ng-transclude>
<child-component name="$ctrl.characters.arya"></child-component>
<child-component name="$ctrl.characters.john"></child-component>
</parent-component>
反映JS
// Parent Component declaration
// /////////////////////////
(function() {
'use strict';
angular
.module('app')
.component("parentComponent", {
transclude: true,
controller: "ParentComponentController",
template:
'<div class="parent-c"></div>'
});
})();
// Parent Component Controller
// /////////////////////////
(function() {
'use strict';
angular
.module('app')
.controller('ParentComponentController', ParentComponentController);
function ParentComponentController() {
var $ctrl = this;
$ctrl.characters = {};
$ctrl.characters.arya = "Arya Stark";
$ctrl.characters.john = "John Snow";
}
})();
//CHILD Component declaration
// /////////////////////////
(function() {
'use strict';
angular
.module('app')
.component("childComponent", {
bindings: {
name: '<'
},
controller: "ChildComponentController",
template:
'<div class="child-c"' +
'<h3>Im a child Component</h3>' +
'<p><strong>Name: </strong>{{$ctrl.name}}</p>' +
'</div>'
});
})();
// CHILD Component Controller
// /////////////////////////
(function() {
'use strict';
angular
.module('app')
.controller('ChildComponentController', ChildComponentController);
function ChildComponentController() {
var $ctrl = this;
}
})();
Check the WORKING SAMPLE on plunkr
要求属性是为组件通信,但我试图用它没有成功:(在这里需要一块光。
你想传递一个名称为组件(通过绑定)的属性或由父(通过要求)继承? – gyc