Connecting 2 controllers and have access to the fi

2019-06-28 02:06发布

问题:

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