Connecting 2 controllers and have access to the first controllers propertie in the second controller

我们两清 提交于 2019-12-10 16:47:39

问题


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

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

first controller

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

second controller

@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(...);
    }
  }
}

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?

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.

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


回答1:


This used to be the AttachAware interface.

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
  }
}


来源:https://stackoverflow.com/questions/26276656/connecting-2-controllers-and-have-access-to-the-first-controllers-propertie-in-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!