简体   繁体   English

将选择的下拉列表绑定到json

[英]Bind chosen dropdown to json

I am kinda new to this, and I can't make it work. 我对此有些陌生,但无法使其正常工作。 I have a chosen dropdown and a call to a rest api on a different domain. 我选择了一个下拉菜单,并调用了另一个域上的rest api。 The cross domain story I solved with a chrome plugin, as it is only temporary, the api and the frontend will be on the same domain, later. 我使用chrome插件解决的跨域故事是暂时的,以后它的api和前端将在同一个域中。 So this is what I have: the directive 这就是我所拥有的:指令

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);
        }
    }
});

the repo service: 回购服务:

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 } }
        })
    }
});

the HTML: 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>

So the first dropdown gets bound, the second one doesn't. 因此,第一个下拉菜单会受到限制,而第二个下拉菜单则不会。 If I use standard selects, they both get bound. 如果我使用标准选择,则它们都会受到约束。 The two console logs look like this: 这两个控制台日志如下所示:

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

so most probably this is the issue. 所以这很可能是问题所在。 same thing happens if I use the data from the promise. 如果我使用诺言中的数据,也会发生同样的事情。

EDIT : if I use an ng-repeat on the manufacturers it works ok: 编辑 :如果我在manufacturers上使用ng-repeat,它可以正常工作:

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

As error clearly suggest 作为错误明确提示

You are assigning $scope.manufacturers is a promise object as per your current implementation. 根据当前的实现,您正在分配$scope.manufacturers是一个$scope.manufacturers对象。

You need to access it data returned by promise hence use following code. 您需要访问由promise返回的数据,因此请使用以下代码。

Use 采用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM