1.4角.在value attr中插入值的类型

angular 1.4.x ngOptions insert the type of the value into value attr

本文关键字:插入 类型 attr 4角 value      更新时间:2023-09-26

我使用的是angularjs 1.4.3,当我使用ngOPtions时,angular会把值的类型插入到value属性中。

你可以在这里看到:http://jsfiddle.net/8bqfq9eb/9

我想要的:插入值没有它的类型。

问题是,ngOptions将值的类型插入到value属性中,并生成如下:number:n (n是一个数字,现在这是id)。

JS:

//used version 1.4.3
var myapp = angular.module('myapp', []);
myapp.controller('FirstCtrl', function ($scope) {
$scope.box = {};
     $scope.box.people = [
        { id: 1, first: 'John', last: 'Rambo', actor: 'Silvester' },
        { id: 2, first: 'Rocky', last: 'Balboa', actor: 'Silvester' },
        { id: 3, first: 'John', last: 'Kimble', actor: 'Arnold' },
        { id: 4, first: 'Ben', last: 'Richards', actor: 'Arnold' }
    ];
    $scope.box.selectedPerson = $scope.box.people[0].id;
});
HTML:

<form method="POST">
<div ng-app="myapp">
    <fieldset ng-controller="FirstCtrl">
        <select name="selectedPerson" id='sel'
            ng-options="p.id as p.first for p in box.people track by p.id"
            ng-model="box.selectedPerson"></select>
        {{ box.selectedPerson }}
        <div class="show" ng-click="show();"></div>
        <input type="submit" />
    </fieldset>
</div>
</form>

你必须添加一个track by来避免这种情况:

    <select name="selectedPerson" id='sel'
        ng-options="p.id as p.first for p in box.people track by p.id"
        ng-model="box.selectedPerson"></select>

但是不管怎样,这种用AngularJS生成HTML的新方法对你的ng-model没有影响。这意味着在ng-model

中使用您的值不会有问题

工作小提琴