【问题标题】:Value of Angular ui-selectAngular ui-select 的值
【发布时间】:2015-07-21 04:29:42
【问题描述】:

在我的应用中有 ui-select:

<ui-select ng-model="flat.flatData.type_local" theme="bootstrap">
   <ui-select-match placeholder="Type">
      {{ $select.selected.type }}
   </ui-select-match>
   <ui-select-choices repeat="t in flat.type_local | filter: $select.search">
      {{ t.type }}
   </ui-select-choices>
</ui-select>

选择了一些东西后,我想将 select 的值添加到数据库中。

在我使用输入之前,添加到数据库没有问题。

<input type="text" class="form-control input-sm" name="type" ng-model="flat.flatData.type_local" placeholder="Type" required>

这是我的控制器的一部分:

angular.module('flatCtrl', ['flatService', 'ui.grid', 'ui.grid.resizeColumns', 'ui.grid.moveColumns', 'ui.grid.autoResize', 'ngSanitize', 'ui.select'])
.controller('FlatController', function(Flat, socketio){

    vm = this;

    vm.createFlat = function(){
        vm.message = '';
        Flat.create(vm.flatData)
            .success(function(data){

                // clear up the form
                vm.flatData = '';
                vm.message = data.message;
            });
    };


    vm.type_local = [
        { type: 'One' },
        { type: 'Two' }
    ];

【问题讨论】:

  • 什么不完全有效?
  • 当我单击提交时,我在数据库中没有此选择的值。没有 flat.flatData.type_local
  • 如果我使用输入,我没有问题
  • 在输入中指定"flat.flatData.type_local" 并查看其flat.type_local,您确定绑定正确
  • 您能否提供更多信息,问题不是很清楚,您所说的数据库是什么意思,正在调用 AJAX 服务并且它没有触发?

标签: javascript angularjs angular-ui-select


【解决方案1】:

这是解决此问题的方法:

 <ui-select-choices repeat="t.type as t in flat.type_local track by $index | filter: $select.search">

 <div ng-bind-html="t.type | highlight: $select.search"></div> 

【讨论】:

  • in

    Selected: {{ flat.flatData.type_local }}

    我有 "Selected: {"type":"One"}" 这可能是问题吗?
  • 你的意思是你有一个预先选择的选项?我对 ui-select 不是很熟悉,但在常规选择中,我会使用 ng-init 来设置预选选项。我更新了jsfiddle。它不影响表单提交。尝试删除并测试它。
  • no, "Selected: {"type":"One"}" 我选择了选项。在您的代码中,您有“已选择:一个”
  • 啊,我明白了。可能是。
  • 你能在 Flat.create(vm.flatData) 之前 console.log 你的数据 vm.flatData。就像我之前说的,可能与您发送的数据有关。
【解决方案2】:
<input type="text" class="form-control input-sm" name="type" 
       ng-model="flat.type_local" placeholder="Type" required>

【讨论】:

  • 输入正常,当我使用选择而不是输入时出现问题
  • 在输入中指定"flat.flatData.type_local" 并查看其flat.type_local,您确定在&lt;ui-select-choices repeat="t in flat.type_local | filter: $select.search"&gt; 中正确绑定
  • flatData 在创建对象时使用 vm.create = function(){ vm.message = ''; Flat.create(vm.flatData) .success(function(data){ // 清除表单 vm.flatData = ''; vm.message = data.message; }); };
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多