简体   繁体   中英

ng-model for select with defined options

I've got a plain select control in my angular app. When page loads I've got no element selected in select control but if I check $scope.data.Method I've got a correct value in int. Is there any way to fix this issue?

<div class="form-group">
    <label class="col-sm-3 control-label">Selected Method:</label>
    <div class="col-sm-4">
        <select class="form-control" ng-model="data.Method">
            <option value="0">Auction</option>
            <option value="1">Delivery</option>
            <option value="2">Range Delivery</option>
            <option value="3">Broadcast</option>
            <option value="4">Self broadcast</option>
            <option value="5">Manual</option>
            <option value="6">Automated</option>
        </select>
    </div>
</div>

Controlers use service to get data. Here is service code for getting data

//Initialize Resource
        this.initResource = function (params) {
            params = params || {};
            params.id = '@id';

            this.resource = $resource('{0}/:id'.format(this.api), params, {
                'update': { method: 'POST' },
                'create': { method: 'PUT' },
                'default': { method: 'GET', params: { id: 'default' } }
            });
        }
//...more code
//Get some data and initialize additional parameters
        this.read = function (id, clone, readonly, version) {
            if (clone == undefined)
                clone = false;

            if (readonly == undefined)
                readonly = false;

            if (this.resource == null) {
                this.initResourceByDefault();
            }

            return this.resource.get({ id: id, clone: clone, readonly: readonly, version: version });
        }

Controller code

function ItemController($scope, $route, $routeParams, $location, $resource, MainCRUDService, ControllerHelper) {
    $scope.noticeType = $routeParams.noticeType;

    MainCRUDService.initResource({ noticeType: $scope.noticeType });
    //Here I am getting data from resourse via service  
    ControllerHelper.initCRUD($scope, MainCRUDService);
}

As I can understand your problem, your service is returning data.Method as an integer value.

But select box will match a string value.

Your problem will get solved if your service will return data.Method as string. Also you can convert your response paramter data.Method to a string value, by using toString() method.

Here is a plunker code which may help you. https://plnkr.co/edit/s0Wrk4?p=preview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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