使用$scope可以轻松地发布一个事件或观看一个事件.
@H_502_1@(function() {
"use strict";
angular
.module("app")
.controller("Ctrl",[
"$scope",CtrlDefinition
]);
function CtrlDefinition($scope) {
$scope.$on("change",listenForChange);
function listenForChange(data) {
//do something
}
}
})();
但是如果我尝试使用var vm =这个语法,我被警告说,$on,$emit和$broadcast不是这个方法.如何访问它们?我还需要在控件定义中注入$scope?
@H_502_1@(function() { "use strict"; angular .module("app") .controller("Ctrl",CtrlDefinition); function CtrlDefinition() { var vm = this; vm.$on("change",listenForChange); //$on is not a method of vm } })();你可以做这样的事情,但是不会破坏不必使用$scope的目的呢?
@H_502_1@(function() { "use strict"; angular .module("app") .controller("Ctrl",CtrlDefinition ]); function CtrlDefinition($scope) { var vm = this; vm.scope = $scope; vm.scope.$on("change",listenForChange); } })();如何使用控制器访问观察者作为语法?