使用ng-repeat字段+ select选项+ $ http.get构建的angularjs表单

时间:2013-11-04 06:50:08

标签: angularjs ng-options

我有这样的代码:

<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>

为什么角度选择不像魅力? 谢谢你的帮助

1 个答案:

答案 0 :(得分: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 }];
        });
    }