我正在使用diepive内部的手表以及名为Selectedize的第三方插件进行困难。

我读了很多关于$ digest / $手表,但仍然存在问题。

我的例子在下面的“works”,但我正在尝试阻止生成的iconicetagcode错误。

可能有更好的方法来接近它,我只是不确定如何。

plunker http://plnkr.co/edit/3jjtseu2blxpwhtw6haw?P=预览

app.directive('selectize', function($parse) {
return {
  restrict: 'A',
  require: ['ngModel'],
  scope: {
    ngModel: '=',
    options: '='
  },
  link: function(scope, el, attrs) {

    var $select = el.selectize({
      valueField: 'id',
      labelField: 'name'
    });

    var selectize = $select[0].selectize;

    // add options
    angular.forEach('options', function(tag) {
      selectize.addOption(tag);
    });

    scope.$watchCollection('options', function(newTags, oldTags) {

      // why are these the same objects?
      console.log('newTags', newTags);
      console.log('oldTags', oldTags);

      if (newTags !== oldTags) {
        // clear options
        selectize.clear();
        selectize.clearOptions();

        // add options
        angular.forEach(newTags, function(tag) {
          selectize.addOption(tag);
        });
      }

    });

    // if value changes without selecting an option,
    // set the option to the new model val
    scope.$watch('ngModel', function(val) {
      console.log('val', val);
      // selectize.setValue(val);
    });
  }
};
});
.

有帮助吗?

解决方案

尝试将呼叫包裹在$超时内的第三方:

$timeout(function() {
    // clear options
    selectize.clear();
    selectize.clearOptions();

    // add options
    angular.forEach(newTags, function(tag) {
      selectize.addOption(tag);
    });

}, 0);
.

,不要忘记注入$超时。

带有零的超时(也是默认值为0的值......),我相信这保证在下一个摘要循环期间运行,从而防止已进入错误。如果这是正确的话,请颂扬,但是在调用一些第三方(Tinymce)JavaScript函数时,我使用了这个技巧来解决摘要错误。

请参阅此帖子所以帖子的解释: Angularjs:在调用$范围时,请防止错误$ digest已在进行中。$ apply()

其他提示

我最近创建了一个指令,用于选择模型和选项的2路绑定。我也不得不使用$超时。

https://github.com/machineboy2045/angular-selectize

http://plnkr.co/edit/twgafu?p=preview

这是指令的基本部分。我删除了完整版本的一些额外功能。

app.directive('selectize', function($timeout) {
  return {
    restrict: 'A',
    require: '^ngModel',
    link: function(scope, element, attrs, ngModel) {

      var config = scope.$eval(attrs.selectize);
      config.options = scope.$eval(attrs.options) || [];
      element.selectize(config);
      var selectize = element[0].selectize;

      selectize.on('option_add', refreshAngularOptions);
      scope.$watch(function(){ return ngModel.$modelValue}, refreshSelectize, true)

      function refreshAngularOptions(value, data) {
        config.options = selectize.options;
      }

      function createOption(opt){
        var newOpt = {};
        newOpt[selectize.settings.valueField] = opt;
        newOpt[selectize.settings.labelField] = opt;
        selectize.addOption(newOpt);
      }

      function refreshSelectize(value){
        $timeout(function(){
          if(angular.isArray(value))
            angular.forEach(value, createOption);
          else
            createOption(value);

          selectize.refreshOptions(false);
          selectize.setValue(value); 
        })
      }
    }
  };
});
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top