如何将 built.io 查询数据添加到 Angularjs $scope

How to add built.io query data to Angularjs $scope

我是 Angular 的新手,正在尝试从 built.io class 获取数据并将其添加到 angular 中的 $scope。我已经完成了所有开始的 Egghead 示例和 AngularJS 教程,但还没有解决这个问题。控制台显示 json 已返回,但我尝试将此 json 添加到 $scope 未成功。我想我应该带着承诺去做这件事,并且已经尝试了几次但没有成功。基本的html是:

<!doctype html>
<html ng-app="Built">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
    <script src="https://raw.githubusercontent.com/raweng/built.io-geoquery-playground/master/js/builtio.js"></script>
    <script src="builtapp.js"></script>
    <link rel="stylesheet" href="bootstrap.css">
    <script>
        Built.initialize('blt90ca3ef1fcb6f354', 'CIC001');
    </script>
</head>

<body ng-controller="MainCtrl">
    <div>
        <div ng-repeat="client in clients">
            {{ client.get('name') }}
        </div>
    </div>
</body>

</html>

和 js:

angular.module('Built', [])
    .controller('MainCtrl', function($scope) {

        $scope.client = null;

        var myQuery = new Built.Query('Client');
        myQuery.exec({
            onSuccess: function(data) {
                // projects is array of Built.Object
                // here's the object we just created
                $scope.clients = data;
            },
            onError: function(err) {
                // error occured
            }
        });

    });

我在 https://jsfiddle.net/o2oxuz12/9/

创建了一个 fiddle

刚刚更新了 fiddle 以包含显示数据项的警报。

angular.module('Built', [])
    .controller('MainCtrl', function($scope) {

        $scope.client = null;

        var myQuery = new Built.Query('Client');
        myQuery.modelType(Built.Object.NONE);
        myQuery.exec({
            onSuccess: function(data) {
                // projects is array of Built.Object
                // here's the object we just created

                $scope.$apply(function(){
                    $scope.clients = data;
                })
            },
            onError: function(err) {
                // error occured
            }
        });

    });

如果其他人遇到同样的问题,我终于在这里找到了答案:AngularJS view not reflecting $scope.model, though data is being set

query.exec({
  onSuccess: function(clients) {
    // projects is array of Built.Object
    // here's the object we just created
    $scope.$apply(function(){
      $scope.clients = clients;
    });
  }
})

已更新 fiddle。 https://jsfiddle.net/o2oxuz12/10/

我会把这个问题留几天没有回答,因为我认为我找到的解决方案有点老套,有人可能有更好的答案。