简体   繁体   English

在路由组件中绑定“&”方法

[英]Binding '&' method in a routed component

Consider this simplified Angular 1.5.x component (all in jsfiddle ): 考虑这个简化的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 (in body) HTML(体内)

<main-component on-update="this.count = count"></main-component>
Value in parent: {{count}}

When clicking the component button, the count variable is being update ('&' onUpdate is binded well). 单击组件按钮时,count变量正在更新('&'onUpdate绑定良好)。

Now I would like to have a route to the component from ui.router: 现在我想从ui.router到组件的路由:

$stateProvider.state({
    name: 'state1',
    component: 'mainComponent',
    url: "#"
  });

Navigating to the state, results in Cannot read property '2' of null , removing the onUpdate member fix the error but break the binding. 导航到状态,导致Cannot read property '2' of null ,删除onUpdate成员可修复错误,但会破坏绑定。

What am I doing wrong? 我究竟做错了什么? What is the way to bind callback methods of components when using ui.router route to components. 使用ui.router路由到组件时,如何绑定组件的回调方法。

jsfiddle jsfiddle

似乎当前不支持使用“ resolve”绑定回调,请参见: https//github.com/angular-ui/ui-router/issues/2793

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM