javascript - Angularjs变量在 Controller 中获取http不可见

标签 javascript angularjs

Controller 中不可见的变量是否仅在 html 中可见?

app.controller('Ctrl', function ($scope, $http) {
    var request = $http({
        method: "get",
        url: "xxxxxx.php"
    })
    .success(function(gl){
        $scope.users = gl;    
    });

    // Initial  data
    var e =  request.users;
    $scope.chartData = [
        ['Coo',e],
    ];
});

最佳答案

$http.success内部构造$scope.chartData,就像

var request = $http({
    method: "get",
    url: "xxxxxx.php"
})
.success(function (gl) {
    $scope.users = gl;

    //initialize your chartData here or 
    //if you have already defined chartData then just push the new object
    $scope.chartData = [['Coo', $scope.users], ];
});

// Initial  data

//this will not work because request is a promise object
/*var e = request.users;
$scope.chartData = [
        ['Coo', e],
];*/

关于javascript - Angularjs变量在 Controller 中获取http不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33327435/

相关文章:

javascript - AngularJS在不同型号上绑定(bind)2个单选按钮组

html - 下拉列表未使用 Angular js 中的选定值进行选择

javascript - 交响乐 1.4 : Call to undefined function visual_effect()

javascript - 更改交换中所选值的文本大小

javascript - 请求 REST 服务

javascript - 语义嵌套 ng-model

javascript - 所有状态的 Angular ui 路由器多个命名 View

javascript - 从 .JSON 访问值时出现问题(可能是路径问题)

javascript - 数据表未捕获类型错误

javascript - 只需要刷新div而不是整个页面