简体   繁体   中英

angular-ui select2 tagging update the data value

So this is my select2 input

<input type="hidden" ng-model="ProjectTagpr" class="input-large" ui-select2="tagOptions" />

where in tagOptions is

{
    data: [{"id":301,"text":"tag2"},{"id":302,"text":"tag2"},{"id":303,"text":"tag3"},{"id":304,"text":"tag4"}],
    multiple: true,
    createSearchChoice: function(term) {
        var timestamp = new Date().getTime();
        return {id: timestamp, text: term, new: true};
    }
}

and ProjectTagpr is

[301, 304];

which renders select2 with tags tag1, tag4 and turns ProjectTagpr to

[{"id":301,"text":"tag2"},{"id":304,"text":"tag4"}]

Now the first question Can I make it so it keeps the original ProjectTagpr structure(just an array of ids, instead of object with both id and text)?

And the second question If I add a new tag, say newtag5 , it would expand ProjectTagpr with something like {id: 1554894854, text: 'newtag5', new: true} , now when I press save and I'd like my $http to update the original data on the select2, so it adds the newtag5 there(of course returned with id from database and without that new: true ), how would I do this?

I tried to do $scope.tagOptions.data = dataReturnedFromHttp; but that obviously(or not) didn't work..

It's actually fairly simple

data: function() {
   return {'results': $scope.data};
}

then it's pretty much live-binded.

It's just that it's somehow undocumented, that you can pass data a function..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM