考虑这个简化的 Angular 1.5.x 组件(全部在jsfiddle中):
appModule.component('mainComponent', {
controller: function() {
var x = 0;
this.broadcast = function() {
this.onUpdate({
count: x++
});
};
this.broadcast();
},
bindings: {
onUpdate: '&'
},
template: '<input type="button" ng-click="$ctrl.broadcast()" value="add"/>'
});
Html(在正文中)
<main-component on-update="this.count = count"></main-component>
Value in parent: {{count}}
单击组件按钮时,计数变量正在更新('&' onUpdate 绑定良好)。
现在我想有一个从 ui.router 到组件的路由:
$stateProvider.state({
name: 'state1',
component: 'mainComponent',
url: "#"
});
导航到状态,结果Cannot read property '2' of null
,删除 onUpdate 成员修复错误但破坏绑定。
我究竟做错了什么?使用ui.router路由到组件时,绑定组件回调方法的方法是什么。