I am using select2 to create a div with tags like functionality when creating a new post.
Stack is Angular 1.6.x
It works well when I am creating a new post BUT when I add pre-selected values when editing the said post, the preselected values never change from the default.
In a nutshell, the values are not binding.
See below:
HTML snippet:
<div class="form-group">
<label for="tags" class="control-label">Tags</label>
<select name="tags" class="tagsSearch" class="form-control" id="tags"
ng-options="tag as tag for tag in post.tags track by tag"
ng-model="post.tags" style="width: 100%" multiple>
</select>
</div>
Note: it looks messy but I got this to work to show my original tags
Controller snippet:
$http.get('/api/post', {params: { title: $scope.title }})
.then(function(res){
$scope.post = res.data.post;
});
$scope.updatePost = function() {
console.log($scope.post.tags);
};
The problem is that the tags do not bind, so if the values are: tag1, tag2, tag3 at rendering and I add: tag4 - updatePost consoles tag1, tag2 and tag3
PS: My tags are an array of strings and have no keys to it like an ID (saw some other post which was referencing to them).
Very lost. Any input would be highly appreciated.
Thanks
EDIT - 28th April 2018:
I have updated my tags to be objects of an array like this:
[
{tag: "tag1", _id: "5ae410756b7a61080cd17c81"},
{tag: "tag2", _id: "5ae410756b7a61080cd17c80"},
{tag: "tag3", _id: "5ae410756b7a61080cd17c7f"}
]
It still doesn't work when I do it like this:
<select name="tags" class="tagsSearch" class="form-control" id="tags"
ng-options="tag as tag.tag for tag in post.tags track by tag._id"
ng-model="post.tags" style="width: 100%" multiple>
</select>
The console.log still only captures the pre-existing tags. New ones are ignored.
track by $index
.it may work – Iaea