Select2 tag input loses AngularJS value

I have an application that switches views using ng-switch, when switching views, my tag input specifically loses its value and falls into its object line, none of my other inputs suffer from this problem:

enter image description here

HTML:

<input ui-select2="version2" 
        id="keywordsGlobal" 
        name="keywordsGlobal" 
        class="region-keywords input-xlarge" 
        data-ng-model="data.keywordsGlobal" 
        required-multiple />

JSON:

[  
   {  
      "id":"[object Object]",
      "text":"[object Object]"
   }
]

Is there any way to prevent this specifically?

+2
source share
2 answers

The next time the selection was loaded, the data did not display correctly. Using initSelect()and manually reassigning the data to choose will appear to fix the problem.

Take a look at http://jsfiddle.net/qdrjk/111/

$scope.version2 = {
    tags : null,
    initSelection: function(elem, callback) {
        console.log(elem);
        var data = $scope.data.keywordsGlobal;
        callback(data);
    },
    createSearchChoice : function(term, data) {
        if ($(data).filter(function() {
            return this.text.localeCompare(term) === 0;
        }).length === 0) {
            return {
                id : term,
                text : term
            };
        }
    }
}
+3
source

ui-select2, , . - angular, select2 , . :

<input type="text" tag-list ng-model="item.tags">

, item.tags - javascript, - . ( ui-select2, 2):

app.directive('tagList', function($timeout) {
  return {
    require: 'ngModel',
    restrict: 'A',
    link: function(scope, element, attrs, controller) {
      scope.$watch(attrs.ngModel, function(value) {
        if (value !== undefined) {
          element.select2('val', value);
        }
      });

      element.bind('change', function() {
        var value = element.select2('val');
        controller.$setViewValue(value);
      });

      $timeout(function() {
        element.select2({
          tags: []
        });
      });
    }
  };
});
+4

All Articles