Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.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 运行和控制器angularjs之间的全局变量_Javascript_Angularjs - Fatal编程技术网

Javascript 运行和控制器angularjs之间的全局变量

Javascript 运行和控制器angularjs之间的全局变量,javascript,angularjs,Javascript,Angularjs,我有一个脚本angularjs,代码如下: var myApp = angular.module('App_example', ['duScroll']) .run(function($rootScope, $location, $window) { var url = $window.location.href; if( url.indexOf("section1") != -1 ) { $rootScope.edition = "section1";

我有一个脚本angularjs,代码如下:

var myApp = angular.module('App_example', ['duScroll'])
.run(function($rootScope, $location, $window) {

    var url = $window.location.href;
    if( url.indexOf("section1") != -1 ) {
        $rootScope.edition = "section1";
    } else {
        if(url.indexOf("section2") != -1) {
            $rootScope.edition = "section2";
        } else if(url.indexOf("section3") != -1) {
            $rootScope.edition = "section3";
        } else {
            $rootScope.edition = "section4";
        }
     }
     if(!history || !history.replaceState) {
         return;
     }
     $rootScope.$on('duScrollspy:becameActive', function($event, $element){
         //Automaticly update location
         var hash = $element.prop('hash');
         if (hash) {
             history.replaceState(null, null, hash+'_'+$rootScope.edition);
         }
     });
});
该控制器:

myApp.controller('ImageController', ['$scope', '$window', '$location', '$document', '$rootScope', function($scope,$window,$location,$document,$state,$rootScope) {

     var url = $window.location.href;
     if( url.indexOf("section1") == -1 ) {
        $rootScope.edition = "section1";
     } else {
        if(url.indexOf("section2") != -1) {
            $rootScope.edition = "section2";
        } else if(url.indexOf("section3") != -1) {
            $rootScope.edition = "section3";
        } else {
            $rootScope.edition = "section4";
        }
     }
});
但是我犯了这个错误,我不知道为什么。如何在运行和控制器之间传递全局变量。它用于在不重新加载的情况下操纵url

TypeError:无法设置未定义的属性“edition”


谢谢。

将所有依赖项按正确顺序添加到函数中,因为您遗漏了一些(如
$state


问题是这条线。数组中有5个参数,但函数中有6个参数。您忘记将
$state
添加到数组中。当您的代码现在就位时,它将
$rootScope
分配给
$state
对象,并且
$rootScope
将不被定义

myApp.controller('ImageController', ['$scope', '$window', '$location', '$document', '$rootScope', function($scope,$window,$location,$document,$state,$rootScope) {
只需将
$state
添加到数组中,您的代码就可以正常工作

myApp.controller('ImageController', ['$scope', '$window', '$location', '$document', '$state', '$rootScope', function($scope,$window,$location,$document,$state,$rootScope) {

myApp.controller('ImageController',['$scope','$window','$location','$document','$state','$rootScope',函数($scope,$window,$location,$document,$state,$rootScope){
数组中的字符串元素必须与函数本身注入的依赖项(作为参数)相匹配:

myApp.controller('ImageController', 
['$scope', '$window', '$location', '$document', '$state', '$rootScope', 
function ($scope, $window, $location, $document, $state, $rootScope) { 
    ... 
}]);
这是因为您使用的是“带注释的依赖项注入”,也就是说,在将依赖项注入控制器之前,您使用字符串显式地命名依赖项。这是避免缩小问题的最佳实践,如下所述:

也就是说,您还可以通过直接传递函数来解决此问题,而无需注释依赖项,如下所示:

myApp.controller('ImageController', 
function ($scope, $window, $location, $document, $state, $rootScope) { 
    ... 
});

您在依赖项定义中遗漏了
$state
依赖项,因此
$rootScope
被分配给参数
$state
。谢谢,但您三个回答我同样的问题:/我将选择第一个响应的人抱歉,但再次感谢您这么快地回复我^^
myApp.controller('ImageController', 
function ($scope, $window, $location, $document, $state, $rootScope) { 
    ... 
});