您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

JavaScript一个AngularJS控制器可以调用另一个吗?

JavaScript一个AngularJS控制器可以调用另一个吗?

控制器之间有多种通信方式。

最好的一种可能是共享服务:

function FirstController(someDataService) 
{
  // use the data service, bind to template...
  // or call methods on someDataService to send a request to server
}

function SecondController(someDataService) 
{
  // has a reference to the same instance of the service
  // so if the service updates state for example, this controller kNows about it
}

另一种方法是在范围内发出事件:

function FirstController($scope) 
{
  $scope.$on('someEvent', function(event, args) {});
  // another controller or even directive
}

function SecondController($scope) 
{
  $scope.$emit('someEvent', args);
}

在这两种情况下,您都可以与任何指令进行通信。

javascript 2022/1/1 18:14:36 有497人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶