【问题标题】:How to set selectize.js value programatically using Angular?如何使用 Angular 以编程方式设置 selectize.js 值?
【发布时间】:2015-01-13 15:28:22
【问题描述】:

我将这个 AngularJS 指令用于selectize.jshttps://github.com/kbanman/selectize-ng

我有两个下拉列表,我想填充其中一个 selectizeVat,具体取决于在另一个 selectizeProducts 中选择的值。我已经阅读了 selectize.js API 并找到了 setValue 方法,但是我该如何使用它呢?

下面是我的模板和控制器代码:

模板

<div ng-controller="dropdownController">
  <select selectize="selectizeProducts" options="products" ng-model="product"></select>
  <select selectize="selectizeVAT" options="vats" ng-model="chosenProduct.vat"></select>
</div>

控制器

app.controller('dropdownController', function($scope, $filter) {
  $scope.chosenProduct = '';
  $scope.vats = [
    {'id': 0, 'vat': '25%'},
    {'id': 1, 'vat': '16%'},  
    {'id': 2, 'vat': '6%'},
  ];
  $scope.selectizeProducts = {
    valueField: 'id',
    labelField: 'name',
    searchField: 'name',
    onChange: function(id) {
      // Get an object from $scope.products by id
      $scope.chosenProduct = $filter('getById')($scope.products, id);
    } 
  };
  $scope.selectizeVat = {
    valueField: 'id',
    labelField: 'vat',
    searchField: 'vat',
  };
});

$scope.products 也是这样的:

{'id': 1, 'name': 'My option', 'nr': 1, 'vat': 0}

基本思想是选择产品,增值税将自动填充到 selectizeVat 下拉列表中。

谢谢!

【问题讨论】:

    标签: javascript jquery angularjs angularjs-directive selectize.js


    【解决方案1】:

    好的,

    所以我找到了自己问题的答案。

    $scope.chosenProduct 需要像这样定义才能填充增值税:

    {'id': 1, 'name': 'My option', 'nr': 1, 'vat': [0]}
    

    似乎您需要为 seletize 定义一个字典,以便它填充您的 &lt;select&gt;

    【讨论】:

      猜你喜欢
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      相关资源
      最近更新 更多