1

Currently I have the following service which populates a table elsewhere in my angular app.

Updated:

'use strict';

    angular.module('projyApp')
      .service('data', function data() {
        // AngularJS will instantiate a singleton by calling "new" on this function             

            var getAllPeeps = function () {
                return peeps;
            }

            var insertPeep = function(peep) {
                peeps.push(peep);
            }
            var getPeep = function(peep) {
                var foundPeep;
                if (peep.firstname) {
                    peeps.forEach(function (pps){
                        if (pps.firstname == peep.firstname) {
                            foundPeep = pps;
                        }
                    });
                } else if (peep.lastname) {
                    peeps.forEach(function (pps){
                        if (pps.lastname == peep.lastname) {
                            foundPeep = pps;
                        }
                    });
                } else if (peep.inumber) {
                    peeps.forEach(function (pps) {
                        if (pps.inumber == peep.tagid) {
                            foundPeep = pps;
                        }
                    });
                }

                if (foundPeep) {
                    return foundPeep;
                } else {
                    return "Error";
                }
            }

            var getDataFromServer = function () {
                //Fake it.
                var peeps = [
                    {firstname:'Buster', lastname:'Bluth', tagid:'01'},
                    {firstname:'John', lastname:'McClane', tagid:'02'},
                    {firstname:'Mister', lastname:'Spock', tagid:'03'}
                ];

                return peeps;
            }

            var peeps = getDataFromServer();

            return {
                getAllPeeps : getAllPeeps,
                insertPeep : insertPeep,
                getPeep : getPeep
            }
      });

Obviously this works, however I want the 'peeps' array to hold objects from an external json file.

How can I maintain functionality while loading the 'peeps' from an external json file like so:

$http.get("../../TestData/peeps.json").success(function(data) {

            });
4

3 回答 3

1

Angular 从一开始就进行 2 路绑定。所以如果你有一个

$scope.var = function() {
$http.get(blah).success($scope.peeps = data.peeps;);
};

它将更新您视图上的 $scope.peeps。$scope.var 通常是一个按钮。

于 2013-08-21T17:33:34.837 回答
0

通常我会以角度返回 $http 承诺。并且来自服务的调用者可以使用成功来做任何它想做的事情。但是为了保持向后兼容性,你可以试试这个:

var getDataFromServer = function () {
        var peeps = [];
        $http.get("../../TestData/peeps.json").success(function(data) {
             peeps.push.apply(peeps,data);
        });

        return peeps;
    }

因此,您将返回一个最初为空的数组。但它将被 json 数组填充。

于 2013-08-21T17:49:21.343 回答
0
function myController($scope, $http){
    $http.get(url).success(function(data){ $scope.people = data; });
}
于 2014-04-10T17:36:53.430 回答