我们如何使用 angular-new-router 在 angular 1.4 中观察控制器内部的表达式
How can we watch expressions inside a controller in angular 1.4 using angular-new-router
由于我们不能在 Angular 1.4+ 中注入 $scope
inside controllers,我们如何才能像以前那样观察表达式 $scope.$watch
?
可以看到试图注入 $scope
here("Could not instantiate controller" 错误),教程告诉我们:
Angular wont be able to instantiate the controller if we pass $scope
in it. It defines its observable properties on this.
(source)
我刚刚发现有一个bug on ngNewRouter causing this problem. It has been fixed,但还没有发布。
要解决这个问题,试试这个:
$ git clone git@github.com:angular/router.git
$ cd router
$ npm install
$ gulp angularify
之后,将 dist 文件复制到您的项目中
我 运行 对此感兴趣并且需要一个 npm 包;暂时让团队中的每个人都从源代码构建是没有好处的。我们在 npm 上发布了一个临时包来解决这个问题:npm install loomio-angular-router@0.5.7
注意:您可能需要将 ng-viewport 重命名为 ng-outlet 以使临时包正常工作。
您在 .activate 中有可用的 $watch。检查下面的内容(从我的应用程序复制粘贴)。
function accountHistoryController (authService, accountFactory, CONSTANTS, $q, $routeParams) {
}
accountHistoryController.prototype.canActivate = function() {
return !!this.authService.isAuthenticated();
}
accountHistoryController.prototype.activate = function($scope) {
console.log ($scope);
$scope.$watch('_this.currentPage', function(newPage){
if (newPage == undefined) return;
});
}
由于我们不能在 Angular 1.4+ 中注入 $scope
inside controllers,我们如何才能像以前那样观察表达式 $scope.$watch
?
可以看到试图注入 $scope
here("Could not instantiate controller" 错误),教程告诉我们:
Angular wont be able to instantiate the controller if we pass $scope in it. It defines its observable properties on this. (source)
我刚刚发现有一个bug on ngNewRouter causing this problem. It has been fixed,但还没有发布。
要解决这个问题,试试这个:
$ git clone git@github.com:angular/router.git
$ cd router
$ npm install
$ gulp angularify
之后,将 dist 文件复制到您的项目中
我 运行 对此感兴趣并且需要一个 npm 包;暂时让团队中的每个人都从源代码构建是没有好处的。我们在 npm 上发布了一个临时包来解决这个问题:npm install loomio-angular-router@0.5.7
注意:您可能需要将 ng-viewport 重命名为 ng-outlet 以使临时包正常工作。
您在 .activate 中有可用的 $watch。检查下面的内容(从我的应用程序复制粘贴)。
function accountHistoryController (authService, accountFactory, CONSTANTS, $q, $routeParams) {
}
accountHistoryController.prototype.canActivate = function() {
return !!this.authService.isAuthenticated();
}
accountHistoryController.prototype.activate = function($scope) {
console.log ($scope);
$scope.$watch('_this.currentPage', function(newPage){
if (newPage == undefined) return;
});
}