2013-11-04 36 views
-1

建我有這樣的代碼:

<form ng-controller="MyCtrl" ng-submit="save()> 
    <div ng-repeat="f in fields"> 
     <label for="theInput">{{ f.label }}</label> 
     <select ng-model="f.value" ng-init="f.value = f.id" ng-options="item.id as item.name for item in f.items"></select> 
    </div> 
</form> 

<script> 
    var Mod = angular.module('myApp', []); 
    function MyCtrl($scope, $http) { 
     var categories = []; 
     $http.get(BASE_URL + 'task/listCategory').success(function(data) { 
      categories = data; 
     }); 
     $scope.fields = [ 
      { label: 'Category', value: 'items', items: categories } 
     ]; 
    } 
</script> 

爲什麼選擇角多年平均值的工作般的魅力? 感謝您的幫助

回答

0

你可能想要做

var Mod = angular.module('myApp', []); 
    function MyCtrl($scope, $http) { 
     var categories = []; 
     $http.get(BASE_URL + 'task/listCategory').success(function(data) { 
      categories = data; 
      $scope.fields = [{ label: 'Category', value: 'items', items: categories }]; 
     }); 
    } 
+0

感謝您的回答,但我在表單有超過1場,還有另一種解決方案? –

+0

這裏的問題是你的'http.get'的成功函數是異步調用的。所以如果您在回調之外使用'data',則可能無法獲取數據。所以我把這個任務放在回調中。如果你有類似的問題,我認爲這種技術可以幫助你。 – Muctadir

+1

一種方式將可能工作是創建空陣列'$ scope.fields = []'第一個角度觀看它...然後將所有數據推入空數組內成功 – charlietfl