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

时间:2015-03-23 05:06:22

标签: angularjs built.io built.io-backend

我是Angular的新手,我正在尝试从built.io类中获取数据并将其添加到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/

创建了一个小提琴

刚刚更新了小提琴,其中包含一个显示数据项的提醒。

2 个答案:

答案 0 :(得分:1)

如果其他人遇到同样的问题,我终于在这里找到答案: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;
    });
  }
})

更新了小提琴。 https://jsfiddle.net/o2oxuz12/10/

我认为这几天没有得到答复,因为我认为我找到的解决方案有点笨拙,有人可能会有更好的答案。

答案 1 :(得分:0)

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
            }
        });

    });