In my project, I have created a model that is linked to several other models. For instance, let's consider a scenario similar to a Stack Overflow question associated with tags. Before making a POST
or PUT
request, the final Object may appear like this:
{
id: 28329332,
title: "checkboxes that append to a model in Angular.js",
tags: [{
id: 5678,
name: "angularjs"
}, {
id: 890,
name: "JavaScript"
}]
}
The controller I have set up so far looks like this:
.controller('CreateQuestionCtrl',
function($scope, $location, Question, Tag) {
$scope.question = new Question();
$scope.page = 1;
$scope.getTags = function() {
Tag.query({ page: $scope.page }, function(data) {
$scope.tags = data;
}, function(err) {
// Handling error when accessing a non-existent page
})
};
$scope.create = function() {
$scope.question.$save(function(data) {
$location.path("/question/" + data.id);
});
};
$scope.$watch($scope.page, $scope.getTags);
}
)
All the available tags are displayed on the page in a paginated manner. The goal is for users to select certain tags and add them to the model for saving.
Is there a way to implement a checkbox interface that updates the $scope.question
with the selected tags from other models?
A recent realization suggests progress made towards the solution:
<div class="checkbox" ng-repeat="tag in tags.objects">
<label><input
type="checkbox"
ng-change="setTag(tag.id)"
ng-model="tag"
> {{ tag.name }}
</div>
Subsequently, in the controller:
$scope.setTag = function(id) {
Tag.get({id: id}, function(data) {
// Further steps required here
})
}