Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 由于在url中追加参数,控制器被调用两次_Javascript_Angularjs_Angular Ui Router - Fatal编程技术网

Javascript 由于在url中追加参数,控制器被调用两次

Javascript 由于在url中追加参数,控制器被调用两次,javascript,angularjs,angular-ui-router,Javascript,Angularjs,Angular Ui Router,我在安格拉斯。我试图使用$location.search('sid',key)在url中追加参数。密钥的值通过http请求来自另一台服务器。 下面是append参数的代码 .config(['$routeProvider',function ($routeProvider) { $routeProvider .when('/', { templateUrl: 'test1.html', con

我在安格拉斯。我试图使用
$location.search('sid',key)在url中追加参数。密钥的值通过http请求来自另一台服务器。
下面是append参数的代码

.config(['$routeProvider',function ($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'test1.html',
                controller: 'test1Controller'
            })
            .when('/message/:userId', {
                templateUrl: 'test2.html',
                controller: 'test2Controller'
            })
            .otherwise({
                redirectTo: '/'
            });
    }])
.run(['$route', '$rootScope', '$location' ,'getAuthDetails', function($route, $rootScope, $location,getAuthDetails) {
        var key;

        getAuthDetails.get().then(function(data){
            key = data.key;
            $rootScope.query = 'sid='+key;
            console.log($location.$$path);
            $location.search('sid', key);  //append parameter. This is calling controller twice
        });

        $rootScope.$on('$routeChangeSuccess', function () {
            $rootScope.query = 'sid='+key;
        });
    }]);
我遇到的问题是。当sid在url中追加时。
console.log
我输入了
test1Controller
两次调用。Sid用于与套接字连接进行连接


url追加后是否有任何方法可以调用控制器。

确保只编写一次控制器。在ng控制器或配置路由中写入:

app.config(['$routeProvider', function($routeProvider){
    $routeProvider.when('/',
        { 
            templateUrl: 'pages/home.html'
            //Remove controller from here
        });
    }]);
home.html

<!-- Add the ng-controller in your view -->
<div ng-controller="MyItemsController">
    <!-- Your stuff -->
</div>


尝试在配置中使用
reloadOnSearch
:false它将阻止控制器加载。

您可以使用
reloadOnSearch:false

例如:

.config(['$routeProvider',function ($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'test1.html',
                controller: 'test1Controller',
                reloadOnSearch: false // wont reload the controller when the search query changes
            })
            .when('/message/:userId', {
                templateUrl: 'test2.html',
                controller: 'test2Controller'
            })
            .otherwise({
                redirectTo: '/'
            });
    }])

我只在索引文件中使用它。我认为是http请求造成的延迟造成了问题。但不是sure@Neilangular中隐藏了很多小功能,即使angular文档也没有提供解释。