ng-select只给出字符串,但我想要整数

Nis*_*ani 37 select angularjs

这是我的角度html文件代码.在我的mongo数据库中frequencyType添加了frequencyType = "1" 但我想要的frequencyType = NumberInt(1);

<div class="span4">
  <select class="span12 combobox" ng-model="frequencyType" required>
    <option value="1">Daily</option>
    <option value="2">Weekly</option>
    <option value="3">Monthly</option>
    <option value="4">Yearly</option>
  </select>
</div>
Run Code Online (Sandbox Code Playgroud)

我进入我的数据库,frequencyType = "1"但我想要frequencyType = NumberInt(1).

Tra*_*rak 60

有一种更简单的方法:

只需使用value*1你的id,将字符串转换为int而不更改值...假设id是一个整数.

所以结果是>

<div ng-app ng-controller="MyCtrl">
    <select ng-model="selectedItem" ng-options="selectedItem*1 as selectedItem for selectedItem in values"></select>
    selectedItem: {{selectedItem}}
</div>
Run Code Online (Sandbox Code Playgroud)

这将在更多情况下起作用,因为indexOf在对象中不可用,仅在数组中可用.此解决方案适用于具有整数作为键的对象(例如,如果缺少某些键,或者如果使用db ID)

  • 我认为这是最好的答案,谢谢 (6认同)
  • 关于键作为整数的问题,如果它不是整数,并且它是一个字符串,那么正在做出更多的假设,因为你信任数据源的顺序 (4认同)
  • 这个问题的正确答案!! 谢谢!! (2认同)

Phi*_*ier 40

这里讨论的大多数解决方案都需要使用ngOptions指令,而不是使用<option>HTML代码中的静态元素,就像OP代码中的情况一样.

Angular 1.6.x

编辑如果您使用Angular 1.6.x,只需使用ng-value指令,它将按预期工作.

angular.module('nonStringSelect', [])
.run(function($rootScope) {
  $rootScope.model = { id: 2 };
})
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.min.js"></script>
<div ng-app="nonStringSelect">
  <select ng-model="model.id">
    <option ng-value="0">Zero</option>
    <option ng-value="1">One</option>
    <option ng-value="2">Two</option>
  </select>
  
  {{ model }}
</div>
Run Code Online (Sandbox Code Playgroud)

旧版本

这里一个方法,使之同时仍然使用静态元素的工作.它显示在AngularJS select指令文档中.

我们的想法是使用指令在模型上注册解析器和格式化程序.选择项时,解析器将执行string-to-int转换,而格式化程序将在模型更改时执行int-to-string转换.

下面的代码(直接来自AngularJS文档页面,不是我的信用).

https://code.angularjs.org/1.4.6/docs/api/ng/directive/select#binding-select-to-a-non-string-value-via-ngmodel-parsing-formatting

angular.module('nonStringSelect', [])
.run(function($rootScope) {
  $rootScope.model = { id: 2 };
})
.directive('convertToNumber', function() {
  return {
    require: 'ngModel',
    link: function(scope, element, attrs, ngModel) {
      ngModel.$parsers.push(function(val) {
        return parseInt(val, 10);
      });
      ngModel.$formatters.push(function(val) {
        return '' + val;
      });
    }
  };
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="nonStringSelect">
  <select ng-model="model.id" convert-to-number>
    <option value="0">Zero</option>
    <option value="1">One</option>
    <option value="2">Two</option>
  </select>
  
  {{ model }}
</div>
Run Code Online (Sandbox Code Playgroud)

  • 好方案!这应该是公认的答案.+1 (2认同)
  • 对于AngularJS 1.6+来说,这绝对是一个更实用的解决方案,因为ng-options因严格的比较而存在问题. (2认同)

And*_*Gis 33

我刚刚遇到了同样的问题并想出了正确的方法 - 不需要"神奇".:)

function MyCtrl($scope) {
    $scope.values = [
        {name : "Daily", id : 1},
        {name : "Weekly", id : 2},
        {name : "Monthly", id : 3},
        {name : "Yearly", id : 4}];
    $scope.selectedItem = $scope.values[0].id;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
<div ng-app ng-controller="MyCtrl">
    <select ng-model="selectedItem" ng-options="selectedItem.id as selectedItem.name for selectedItem in values"></select>
    selectedItem: {{selectedItem}}
</div>
Run Code Online (Sandbox Code Playgroud)

  • 这是正确的答案,忘记其他解决方案 (2认同)

All*_*ile 12

我认为这样做的角度方法是在select标签中使用指令"convert-to-number".

<select class="span12 combobox" ng-model="frequencyType" required convert-to-number>
    <option value="1">Daily</option>
    <option value="2">Weekly</option>
    <option value="3">Monthly</option>
    <option value="4">Yearly</option>
</select>
Run Code Online (Sandbox Code Playgroud)

您可以在页面末尾看到文档和小提琴:https://docs.angularjs.org/api/ng/directive/select


Max*_*tin 10

我建议你尝试使用indexOf.

JS

function MyCtrl($scope) {
    $scope.values = ["Daily","Weekly","Monthly","Yearly"];
    $scope.selectedItem = 0;
}
Run Code Online (Sandbox Code Playgroud)

HTML

<div ng-app ng-controller="MyCtrl">
    <select ng-model="selectedItem" ng-options="values.indexOf(selectedItem) as selectedItem for selectedItem in values"></select>
    selectedItem: {{selectedItem}}
</div>
Run Code Online (Sandbox Code Playgroud)

演示 Fiddle

所以发送到mongoDB $scope.selectedItem


lep*_*epe 8

这是我到目前为止找到的最简单的方法:

function Controller($scope) {
    $scope.options = {
        "First option" : 1,
        "Second option" : 2,
        "Last option" : 10
    }
    $scope.myoption = 2;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="Controller">    
    <select ng-model="myoption" ng-options="label for (label, value) in options"></select>
    Option Selected: {{myoption}}
</div>
Run Code Online (Sandbox Code Playgroud)

或者,如果要使用数字索引:

function Controller($scope) {
    $scope.options = {
        1 : "First option",
        2 : "Second option",
        10 : "Last option"
    }
    $scope.myoption = 2;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="Controller">    
    <select ng-model="myoption" ng-options="value*1 as label for (value, label) in options"></select>
    Option Selected: {{myoption}}
</div>
Run Code Online (Sandbox Code Playgroud)

对于Angular 2,请关注rabit ...