gpt4 book ai didi

javascript - 这个例子中 "main" Controller 是如何访问$routeParams的?

转载 作者:行者123 更新时间:2023-11-30 12:45:10 25 4
gpt4 key购买 nike

我正在试验 $routeParams,跟随 this example在 AngularJS 文档中。作为引用,这里是脚本本身:

 angular.module('ngRouteExample', ['ngRoute'])

.controller('MainController', function($scope, $route, $routeParams, $location) {
$scope.$route = $route;
$scope.$location = $location;
$scope.$routeParams = $routeParams;
})

.controller('BookController', function($scope, $routeParams) {
$scope.name = "BookController";
$scope.params = $routeParams;
})

.controller('ChapterController', function($scope, $routeParams) {
$scope.name = "ChapterController";
$scope.params = $routeParams;
})

.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/Book/:bookId', {
templateUrl: 'book.html',
controller: 'BookController',
resolve: {
// I will cause a 1 second delay
delay: function($q, $timeout) {
var delay = $q.defer();
$timeout(delay.resolve, 1000);
return delay.promise;
}
}
})
.when('/Book/:bookId/ch/:chapterId', {
templateUrl: 'chapter.html',
controller: 'ChapterController'
});

// configure html5 to get links working on jsfiddle
$locationProvider.html5Mode(true);
});

我无法理解的是:MainController 如何获取更新的 $routeParam 对象? 我可以在单击时看到它, MainController 负责设置的项目正在更改,但我不明白如何。重现此行为有点困难。

最佳答案

它不会重新实例化,也不会“获取”更新后的 $routeParams 对象 - Controller 中的对象是 the routeParams 对象。它是其他 Controller 中的相同对象,而不是它的“副本”。

因此,当 $routeParams 对象发生变化时,另一个 Controller 已经有了变化。

关于javascript - 这个例子中 "main" Controller 是如何访问$routeParams的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22773297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com