首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将built.io查询数据添加到Angularjs $scope

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

Stack Overflow用户
提问于 2015-03-23 05:06:22
回答 2查看 202关注 0票数 1

我对角很陌生,并试图从built.io类中获取数据,并将其添加到$scope中。我已经完成了所有最初的Egghead示例和AngularJS教程,但还没有解决这个问题。控制台显示返回了json,但是我将这个json添加到$scope的尝试没有成功。我想我应该用一个承诺来做这件事,并且在没有运气的情况下尝试了好几次。基本的html是:

代码语言:javascript
复制
<!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:

代码语言:javascript
复制
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/

刚刚更新了小提琴,包括一个显示一项数据的警报。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-24 02:49:44

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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-03-23 06:40:19

代码语言:javascript
复制
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
            }
        });

    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29203614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档