【发布时间】:2015-02-13 10:15:39
【问题描述】:
如果我想在 Angular 中使用“Controller as ...”语法,我应该如何处理需要放入控制器中的 $scope.$on(...) 之类的东西?
我有一种印象,除了下面显示的方法之外,我还可以采用其他方法。 在这里,为了让 $scope.$on 工作,我将“this”绑定到回调函数。我试图在控制器内的“this”上调用 $on,但它没有用。
你能在这里给我一个提示吗,或者如果我完全搞砸了,你能给我指出一些正确的方法吗?谢谢。
main.js:
angular.module('ramaApp')
.controller('MainCtrl', ['$scope', '$location', function ($scope, $location) {
this.whereAmINow = 'INDEX';
$scope.$on('$locationChangeStart', function(event) {
this.whereAmINow = $location.path();
}.bind(this));
this.jumpTo = function(where) { $location.path(where); }
}]);
index.html:
<div ng-controller="MainCtrl as main">
<p>I am seeing the slide named: {{ main.whereAmINow }}</p>
<div ng-click="main.jumpTo('/slide1')">Slide 1</div>
<div ng-click="main.jumpTo('/slide2')">Slide 2</div>
<div ng-click="main.jumpTo('/slide3')">Slide 3</div>
</div>
【问题讨论】: