【问题标题】:How to add built.io query data to Angularjs $scope如何将 built.io 查询数据添加到 Angularjs $scope
【发布时间】:2015-05-26 01:13:59
【问题描述】:

我是 Angular 的新手,正在尝试从 built.io 类中获取数据并将其添加到 $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 以包含一个显示数据项的警报。

【问题讨论】:

  • 你的小提琴不工作..请检查
  • 嗨,问题是代码不起作用,因为在执行 ng-repeat 时 $scope.clients 没有价值。小提琴设置正确并正在接收数据。您可以通过添加警报(数据)进行测试;在成功功能中。这是我希望解决的问题。
  • 您的小提琴无法正常工作,因为它无法加载 builtio.js 文件,因为它的 mime 类型是 text/html
  • builtio.js 文件设置了built.io 的数据导入。没有它,就没有数据。如果您在加载小提琴时检查检查器中网络选项卡中的响应,您可以看到数据。

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


【解决方案1】:
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
            }
        });

    });

【讨论】:

  • 这仍然不适合我。当您这样做时,这是否会在 Fiddle 中显示客户端名称?请随时更新小提琴。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 2017-12-06
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    相关资源
    最近更新 更多