gpt4 book ai didi

javascript - $scope.$on ('$viewContentLoaded' ,.. 调用两次以上

转载 作者:行者123 更新时间:2023-12-02 14:32:46 26 4
gpt4 key购买 nike

例如,我有这样简单的代码片段:

var sampleApp = angular.module('sampleApp', []);
sampleApp.config(['$routeProvider',
function($routeProvider, $rootScope) {
$routeProvider.
when('/AddNewOrder', {
templateUrl: 'add_order.html',
controller: 'AddOrderController'
}).
when('/ShowOrders', {
templateUrl: 'show_orders.html',
controller: 'ShowOrdersController'
}).
otherwise({
redirectTo: '/AddNewOrder'
});
}]);

sampleApp.controller('AddOrderController', function($scope, $rootScope) {
$scope.message = 'This is Add new order screen';

$scope.$on('$viewContentLoaded', function (event, toState, toParams, fromState, fromParams) {
console.log("Add new order");
});

});

sampleApp.controller('ShowOrdersController', function($scope,$rootScope) {
$scope.message = 'This is Show orders screen';

$scope.$on('$viewContentLoaded', function (event, toState, toParams, fromState, fromParams) {
console.log("Show orders");
});
});

它是 ng 示例中的 app.js,在浏览器中它的示例如下所示:

enter image description here

完整版本您可以在这里看到:http://plnkr.co/edit/jc3nFBAaBLMiGMYV4TZW?p=preview

哪里有些奇怪:

$scope.$on('$viewContentLoaded', function (event, toState, toParams, fromState, fromParams) {
console.log("Add new order");

$scope.$on('$viewContentLoaded', function (event, toState, toParams, fromState, fromParams) {
console.log("Show orders");
});

每次点击链接时都会调用两次。我也尝试使用 $routeChangeSuccess,但结果是相同的。为什么会出现这种情况?需要做什么才能获得 $scope.$on(..) 的单次调用?

最佳答案

href="#AddNewOrder" 更改为 href="#/AddNewOrder"。因此,您的 Controller 初始化了两次。

关于javascript - $scope.$on ('$viewContentLoaded' ,.. 调用两次以上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37692772/

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