如何使用AngularJS更改全局变量的值

时间:2016-06-14 18:48:02

标签: javascript angularjs ajax

我是AngularJS和图表的新手。我在ajax调用中定义了x和y值。但是,我发现只有在$ ajax调用之外定义的$ scope.categories和$ scope.dataset(这些是x和y值)时,才能显示该图。也就是说,这些变量只能在下面的代码片段中的解释位置定义。 但是,我不知道如何将其值更改为全局变量。我想这可以用范围完成,但我不知道如何......

以下是我的代码的一部分:

app.controller("MyController", function($scope,$http){
    var newArray=[];
    var vArray=[];
    $http.get("model/select.php").success(function(data){

        /*Define newArray and vArray's value as what read from database 
        using php here*/
        $scope.categories = [{"category":newArray}]
        $scope.dataset = [{"data":vArray}]


    }).error(function(){
        alert("An unexpected error occurred!");
    });
 /*$scope.attr, $scope.categories, $scope.dataset can be only defined here, out side of $http() and inside controller*/

});

1 个答案:

答案 0 :(得分:0)

你试过$rootScope吗?查看$ rootScope的Angular JS文档,您可以使用它更改其他控制器中的值。