在angularjs中访问控制器外部的变量

时间:2016-11-22 10:09:00

标签: javascript angularjs

我想访问或者更确切地说使用在我的控制器中修改和初始化的变量,在该控制器之外作为常规变量。

这是控制器:

 my.app.controller('queryCtrl', ['$scope','$http',function($scope,$http){
var tempAstData={
    'rightNode': 'longitude = 72.8604836',
    'leftNode': {
        'rightNode': 'latitude = 19.1738427',
        'leftNode': "fence_brand_name = 'taco'",
        'centerOperator': 'AND'
    },
    'centerOperator': 'AND'
}
// some functions on it
}]);
 //normal javascript 
// want to use tempAstData here as plain java script in this same file....
//this part is not a controller or anyway related t angular js . This is plane java script.

我是棱角分明的新人,感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

var myGlobalVariable = null;
 my.app.controller('queryCtrl', ['$scope','$http',function($scope,$http){
var tempAstData={
    'rightNode': 'longitude = 72.8604836',
    'leftNode': {
        'rightNode': 'latitude = 19.1738427',
        'leftNode': "fence_brand_name = 'taco'",
        'centerOperator': 'AND'
    },
    'centerOperator': 'AND'
}

myGlobalVariable = tempAstData;
// some functions on it
}]);

alert(myGlobalVariable);

 //normal javascript 
// want to use tempAstData here as plain java script in this same file....
//this part is not a controller or anyway related t angular js . This is plane java script.

答案 1 :(得分:0)

使用$rootScope以角度方式全局引用数据

my.app.controller('queryCtrl', ['$scope','$http','$rootScope',function($scope,$http,$rootScope){  

var tempAstData={
    'rightNode': 'longitude = 72.8604836',
    'leftNode': {
    'rightNode': 'latitude = 19.1738427',
    'leftNode': "fence_brand_name = 'taco'",
    'centerOperator': 'AND'
    },
    'centerOperator': 'AND'
  }

 $rootScope.tempAstData = tempAstData;
// some functions on it
}]);

然后您可以将$rootScope.tempAstData用于其他控制器

my.app.controller('otherCtrl', ['$scope','$http','$rootScope',function($scope,$http,$rootScope){  
 console.log($rootScope.tempAstData) 
})

答案 2 :(得分:0)

正如@Subhash在这个问题的评论中提到的,他在你的控制器中有3种混合方法。然后他需要在这个assync方法之后操作你的tempAstData变量:

  

@trichetriche我的控制器中有3个asyn方法可以访问服务器   有POST请求。我没有在这里张贴以避免并发症。要么   确切地说我想用我的html文件访问那个tempAstData   script tag new Treant(tempAstData)

在这种情况下,我建议使用$q等待所有承诺,并在此调用后调用一个操纵tempAstData的方法,如下所示:

my.app.controller('queryCtrl', ['$scope','$http', '$q',function($scope, $http, $q) {

    var wrapMethod = function (parameter) {
        // do something with parameter
    }

    var tempAstData={
       'rightNode': 'longitude = 72.8604836',
       'leftNode': {
           'rightNode': 'latitude = 19.1738427',
           'leftNode': "fence_brand_name = 'taco'",
           'centerOperator': 'AND'
       },
       'centerOperator': 'AND'
    }

    var promises = [];

    // add promises returned by assync methods to a array
    promises.push(assyncMethod1());
    promises.push(assyncMethod2());
    promises.push(assyncMethod3());


    // wait all promises to be resolved
    $q.all(promises).then(function () {

        // call wrap method with 'normal' JavaScript code previously in a script tag
        wrapMethod(tempAstData);

    })

}]);

@Subhash请编辑您的问题并添加完整信息以澄清其他用户