在角控制器内呼叫控制器控制器

2023-09-13 02:45:54 作者:男人必须傲ゞ

我有一个弹出式的控制器:

I have a popup controller:

@PopupController = ["$scope", ($scope) ->
  $scope.isActive = false

  $scope.open = ->
    $scope.isActive = true

]

和老师控制器:

@TeachersController = ["$scope", "Teacher", ($scope, Teacher) ->
  $scope.teachers = Teacher.query()

  $scope.showTeacher = ->
    # somehow call PopupController.open()

]

如何调 PopupController.open() TeachersController

或者,也许我这样做是错误和角度有一些更好的方法来实现它?

Or maybe I'm doing it wrong and Angular has some better ways to implement it?

推荐答案

您可以使用的 $广播(向下子作用域)或$排放(向上范围层次),以一个控制器发送一个事件的形式向其他。

You may use $broadcast (downward to child scopes) or $emit (upward to scope hierarchy) to transmit an event form one controller to the other.

然后,你会在登记触发你的函数接收的事件。请参见 http://docs.angularjs.org/api/ng.$ro​​otScope .Scope#$上。

Then you would register an event on the receiver that triggers your function. See http://docs.angularjs.org/api/ng.$rootScope.Scope#$on.