将选择的下拉列入json

时间:2015-02-03 09:48:54

标签: jquery angularjs jquery-chosen

我对此有点新意,我无法使其发挥作用。我有一个选定的下拉列表,并在另一个域上调用rest api。我用chrome插件解决的跨域故事,因为它只是暂时的,api和前端将在同一个域上,稍后。 所以这就是我所拥有的: 指令

app.directive('filterSection', function () {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: 'app/users/filterSection.html',
        controller: function ($scope, reposSvc, toastr) {
            $scope.selectedManufacturerId = {};
            $scope.manufacturers = [];

            $scope.manufacturers1 = [{ id: 1, name: 'abc'}];
            $scope.manufacturers = reposSvc.manufacturer.query(
                function (data) {
                },
                function () {
                    toastr.error("Some error message");
                }
            ).$promise.then(function () {
                $scope.manufacturers = [{ id: 2, name: 'def' }];
            });

            console.log($scope.manufacturers);
            console.log($scope.manufacturers1);
        }
    }
});
回购服务:

app.service('reposSvc', function ($resource, noLimitsAdminUrl) {
    return {
        //MANUFACTURERS
        manufacturer: $resource(baseUrl + 'manufacturer/:id', null, {
            'get': { method: 'GET', params: { id: null } },
            'update': { method: 'PUT', params: { user: null } },
            'insert': { method: 'POST', params: { user: null } },
            'delete': { method: 'DELETE', params: { id: null } }
        })
    }
});

HTML:

<div class="form-group">
        <label for="manufacturers1" class="col-xs-3 col-sm-3 col-md-3 control-label">Manufacturers</label>
        <div class="col-xs-5 col-sm-5 col-md-5">
            <select class="form-control" style="display:inline-block" name="manufacturers1" ng-model="selectedManufacturerId" ng-options="m.name for m in manufacturers1" chosen required>
                <option value="">choose manufacturer</option>
            </select>
        </div>
    </div>
    <div class="form-group ">
        <label for="manufacturers" class="col-xs-3 col-sm-3 col-md-3 control-label">Manufacturers</label>
        <div class="col-xs-5 col-sm-5 col-md-5">
            <select class="form-control" style="display:inline-block" name="manufacturers" ng-model="selectedManufacturerId" ng-options="m.name for m in manufacturers"  chosen required>
                <option value="">choose manufacturer</option>
            </select>
        </div>
    </div>

所以第一个下拉列表会被绑定,第二个下拉列表不会被绑定。如果我使用标准选择,他们都会受到约束。 两个控制台日志如下所示:

1: `Promise {$$state: Object, then: function, catch: function, finally: function}`
2:[Object]

所以很可能这就是问题所在。如果我使用诺言中的数据,就会发生同样的事情。

编辑:如果我在manufacturers上使用ng-repeat就可以了:

<ul>
    <li ng-repeat="manu in manufacturers">{{manu.name}}</li>
</ul>

1 个答案:

答案 0 :(得分:0)

由于错误明确提示

您根据当前的实施方式分配$scope.manufacturers是一个承诺对象。

您需要访问由promise返回的数据,因此请使用以下代码。

使用

reposSvc.manufacturer.query(function(data) {
    $scope.manufacturers = data;
}, function() {
    toastr.error("Some error message");
});