【问题标题】:Fire event after Angular binds/populates dropdown using ng-optionsAngular 使用 ng-options 绑定/填充下拉列表后触发事件
【发布时间】:2017-04-18 18:02:11
【问题描述】:

我正在使用引导多选控件。一旦 Angular 填充了所有选项,我需要在选择元素上执行 multiselect()。

这是绑定到 $scope 属性的选择框,在我看来,选项是从 accountInfo 对象数组 sn-p 生成的:

<select id="property" multiple="multiple" 
    ng-model="selectedProperty" 
    ng-options="account.property_name group by account.client_name for account in accountInfo">
</select>

所以一旦选项存在,我需要调用 $("#property").multiselect() 来生成引导控件,但我正在寻找一个事件来监听它告诉我何时生成了所有选项.

【问题讨论】:

  • ng-repeat finish event的可能重复
  • 您应该为此使用指令。另外,您确定&lt;select multiple="multiple"&gt; 可以与ng-modelng-options 一起使用吗?我可以发誓我过去遇到过问题
  • ng-model 只要模型是一个数组,就可以与选择多个井一起工作 - 如何构建指令以确定何时写入最后一个选项?

标签: javascript angularjs twitter-bootstrap bootstrap-multiselect


【解决方案1】:

不要在控制器中初始化引导多选控件,而是在指令link() 函数中执行此操作,该函数在相应元素呈现后调用。

app.directive('multiselect', function() {
  return {
    restrict: 'A',
    link: function(scope, element) {
      element.multiselect();
    }
  };
});

这是demo

【讨论】:

  • 谢谢 - 我会给你信用,因为它确实回答了书面问题。我应该提到绑定到 ddl 的数据是异步的,并且在链接函数执行后很长时间才绑定。我已经将其编码为使用 $interval 来检查模型数据是否存在,但如果您有更好的想法,请随时告诉我。
猜你喜欢
  • 1970-01-01
  • 2017-10-27
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多