如何对RESTful服务返回的对象执行ng-repeat

时间:2015-09-29 17:38:49

标签: angularjs rest ng-repeat

我有一个RESTful服务,我正在调用它来返回数据。我已经获得了要返回的数据对象,并且可以从构造的URL中查看数据。现在我需要对该数据执行ng-repeat并在提交时显示div。有点卡住了。这是我的代码。

我确信这很简单。

非常感谢

(我有适当的依赖)

<-- Service-->

var StoreUrl = configurationService.StoreUrl;

return {
    GetStore: function(StoreNum, City, State, StoreName, Zip) {
            console.log(StoreUrl + "/GetStoreList.ashx?StoreNumber=" + StoreNum + "&StoreCity=" + City + "&StoreState=" + State + "&StoreName=" + StoreName + "&ZipCode=" + Zip );
        return $httpq.get(StoreUrl + "/GetStoreList.ashx?StoreNumber=" + StoreNum + "&StoreCity=" + City + "&StoreState=" + State + "&StoreName=" + StoreName + "&ZipCode=" + Zip );
    }
};

<--Controller-->

$scope.StoreNumber = '';
$scope.StoreState = '';
$scope.StoreCity = '';
$scope.StoreName = '';
$scope.ZipCode = '';
$scope.stores = [];
$scope.getStores = function(){
storeLookupService.GetStores($scope.StoreNumber, $scope.StoreCity, $scope.StoreState, $scope.StoreName, $scope.ZipCode).then(function (result){
    if (result.StatusCode == "SUCCESS") {
        $scope.stores = result.stores;
    } else {
        alertify.alert(configurationService.ErrorMessage("finding stores", result.StatusMessage));
    }
});
};

<--View-->

<div class="col-lg-push-12 store-lookup">
        <form class="form-horizontal" name="storeLookup" >
            <label>Group Number:</label>
            <input  class="form-text" type="text" ng-model="StoreNumber" name="submitted[group]" id="edit-submitted-group">
            <label>Pharmacy Name:</label>
            <input class="form-text" type="text" ng-model="StoreName" name="submitted[store]" id="edit-submitted-store">
            <label>City:</label>
            <input  class="form-text" type="text" ng-model="StoreCity" name="submitted[city]" id="edit-submitted-city">
            <label>State: </label>
            <select id="state" class="form-control" ng-model="StoreState" ng-options="state.StateAbbr as state.StateName for state in StoreState" tabindex="15">
                <option value="">Pick a state</option>
            </select>
            <label>Zip:</label>
            <input class="form-text" type="text" ng-model="ZipCode" name="submitted[zip]" id="edit-submitted-zip">
            <label>24 Hour: </label>
            <input class="form-checkbox" type="checkbox" name="24hronly" id="edit-submitted-checkbox">
            <input id="edit-submit1" type="submit" value="Submit" ng-click="getStores()">

        </form>
    </div>
    <li class="storeresults" ng-repeat="store in Stores">{{store.StoreName}}</li>

该服务返回一个对象结构,如:

{"Stores":[{"StoreNum":"","StoreEmployerName":"","StoreName":"MK123","Address":"XXX","City":"XXX","State":"XX","Zip":"XXX","Phone":"(XXX) XXX-XXXX","Fax":"(XXX) XXX-XXX","Is24Hour":"0","Distance":"0","RecordNum":"1"}

4 个答案:

答案 0 :(得分:1)

//Change this
$scope.stores = result.stores
//To This
$scope.Stores = result.Stores;

答案 1 :(得分:1)

感谢所有建议。实际上我不得不从服务中调用对象以及来自我服务的对象。

<li class="storeresults" ng-repeat="store in locationStores.Stores">{{store.StoreName}}</li>

答案 2 :(得分:0)

这样的事情应该有效:

<div ng-repeat="item in $scope.stores">
{{item.StoreNum}}
</div>

答案 3 :(得分:0)

请尝试以下操作,

<li class="storeresults" ng-repeat="store in controllername.stores">{{store.StoreName}}</li>

因为您正在将服务中的对象定义为控制器中的"stores"