【发布时间】:2016-08-31 20:17:54
【问题描述】:
首先,我使用的是components。
我有这个“父母”component:
(function() {
'use strict';
angular
.module('parentModule', [])
.component('parent', {
templateUrl: 'parent.tpl.html',
controller: ParentCtrl,
transclude: true,
bindings: {
item: '='
}
});
function ParentCtrl() {
var vm = this;
vm.item = {
'id': 1,
'name': 'test'
};
}
})();
我只是想与另一个组件共享object item,如下所示:
(function() {
'use strict';
angular
.module('childModule', [])
.component('child', {
templateUrl: 'child.tpl.html',
controller: ChildCtrl,
require: {
parent: '^item'
}
});
function ChildCtrl() {
console.log(this.parent)
var vm = this;
}
})();
查看(父):
Parent Component:
<h1 ng-bind='$ctrl.item.name'></h1>
<child></child>
查看(儿童):
Child component:
Here I want to print the test that is in the parent component
<h2 ng-bind='$ctrl.item.name'></h2>
实际上我收到以下错误:
与指令一起使用的属性“项目”中的表达式“未定义” 'parent' 是不可赋值的!
这里是DEMO 以更好地说明情况
你能告诉我如何让它工作吗?
【问题讨论】:
标签: javascript angularjs angularjs-components