连接2个控制器,并访问第二个控制器中的第一个控制器属性 [英] Connecting 2 controllers and have access to the first controllers propertie in the second controller

查看:237
本文介绍了连接2个控制器,并访问第二个控制器中的第一个控制器属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个角度飞镖的问题。 1个html文件触发范围和2个控制器类
index.html

i have a problem with angular dart. 1 html file to trigger scopes and 2 controller classes index.html


...

$ b b {{subCtrl.user.name}}


...

... {{subCtrl.user.name}} ...

第一个控制器

@Controller(
  selector: '[mainController]',
  publishAs: 'mainCtrl'
)
class MainController{
  User user = new User('testuser');
  MainController();
}

第二个控制器

@Controller(
  selector: '[subController]',
  publishAs: 'subCtrl'
)
class SubController{

  @NgOneWay('user')
  User user;

  // constructor
  SubController(){
    getData();
  }

  void getData(){
    if(user != null){
      // following code is not exececutet, because user is null
      httpRequst(...);
    }
  }
}

设置@NgOneWay?似乎不是在构造函数完成之前。我在哪里必须调用我的方法?

when is the time user is set over @NgOneWay? seems like not before the constructor is finished. where do i have to call my method?

现在我有问题,我必须在SubController类的getData函数中做一个异步请求。这个http请求需要,即user.name属性来构建域,但用户在构建函数中启动时不是活动的。我不能将认证设置到第二控制器。

now i have the problem i have to make a asynch request in the getData function in the SubController class. this http request needs i.e the user.name propertie to build the domain, but user is not active when i start it in the constructor. i cant set the authentification to the second controller. there must be another option to get this working.

我尝试过几个dart的未来,但没有让我为一个属性工作。

i tried several things with dart's future, but did not get i working for a propertie.

推荐答案

这过去是 AttachAware 接口。

class SubController implements AttachAware {
  attach() {
    getData();
    // or new Future(() => getData()); // if the line above still doesn't work - to give Angular one additional cycle to finish
  }
}

这篇关于连接2个控制器,并访问第二个控制器中的第一个控制器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆