javascript - 将数据传递到另一个作用域

标签 javascript angularjs

这就是我所拥有的:

$http.get("http://localhost/app/api/Suppliers").success(function(response) {
    $scope.dataSource = response;
    console.log($scope.dataSource);
    $scope.safeApply(function() {
        $scope.settings.columns[3] = $scope.dataSource;
    });
}); 


$scope.settings  = {
    colHeaders: ["Code", "Comments"],
    contextMenu : ["row_above","row_below","remove_row"],
    colWidths: [100, 100],
    columns : [
        {   type: 'dropdown',
            source: ['Not Started', 'In Progress', 'Completed']
        },
        {},
        {},
        {   type: 'dropdown',
            source: $scope.dataSource,
        }
    ]
};

问题是 $scope.dataSource 未定义,它不显示数据。应该怎样解决这个问题?

更新: 这将显示我的 $http 调用中的数据。但是在设置源中,当我调用源时: $scope.dataSource 未定义

最佳答案

当您向服务器发出请求时, Controller 的其余部分将被编译,然后您将进入成功函数

您在请求的成功内声明您的数组,最好像这样在外部声明

$scope.dataSource = []

$http.get("http://localhost/app/api/Suppliers").success(function(response) {
    $scope.dataSource = response;
    console.log($scope.dataSource);
    $scope.safeApply(function() {
        $scope.settings.columns[3] = $scope.dataSource;
    });
}); 

更新

试试这个

$http.get("http://localhost/app/api/Suppliers").success(function(response) {
      $scope.dataSource = response;
      console.log($scope.dataSource);

      getSetting();

      $scope.safeApply(function() {
        $scope.settings.columns[3] = $scope.dataSource;
      });
    });


    var getSetting = function() {
      $scope.settings = {
        colHeaders: ["Code", "Comments"],
        contextMenu: ["row_above", "row_below", "remove_row"],
        colWidths: [100, 100],
        columns: [{
          type: 'dropdown',
          source: ['Not Started', 'In Progress', 'Completed']
        }, {}, {}, {
          type: 'dropdown',
          source: $scope.dataSource,
        }]
      };
    }

关于javascript - 将数据传递到另一个作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36569821/

相关文章:

javascript - 为每个路径都在 raphael.js 集合中的不同路径应用不同的颜色

javascript - 代码签名 Javascript 以使用 'new ActiveXObject()'/'GetObject()'

javascript - 如何在第一个 div 中包装文本,但使用第二个 div 的宽度应根据内容调整的可用空间?

javascript - ng-maps 方向替代方案

angularjs - $routeParams 在主 Controller 中为空

javascript - 在 WooCommerce 单个产品页面上专门加载脚本

javascript - 将 window.location.href 与 Jquery Mobile 和 RequireJS 一起使用时如何避免出现空白页?

angularjs - 带有包含 "dot"的 url 查询参数的 Angular ui-router

css - Yo Angular 没有带 Bootstrap 样式的应用程序

javascript - 定义 AngularJS 教程中使用的术语 "routing"?