I have an AngularJS frontend and REST (Jersey) backend that reads a list of book titles a teacher has assigned to a Student. There are two views/pages - list and edit. A list page is a table that shows Student Name and Currently Assigned Book Title.
A teacher can select to edit the book assigned to a student. On selecting "Edit", teacher is taken to the Edit view/page that has a dropdown containing list of available book titles to read in the library and is populated by calling REST service Books.getUnassigned(). When a book is assigned to any student the backend will remove the book title from the unassigned list and add it to assigned list (Books.getAssigned()). So on the edit page I need to call the service, push the current assigned book title to that array and also set the default to the current book title.
<select ng-model="bookToAssign">
<option ng-repeat="book in available_books" value="{{book}}">{{book}}</option>
</select>
Below is my service call. When the edit page is loaded $rootScope.args.arg contains a json object with all the data that needs to be passed from the previous page to the edit page - verified that the object is correct.
$scope.available_books= NaiSvc.getUnassignedBooks.query(
function(data){
alert('success, got data: ', data);
if($rootScope.args.arg !=null){
data.push($rootScope.args.arg); //verified this gets the right object
$scope.bookToAssign.setDefault = data[data.length-1];
}
}, function(err){
alert('request failed');
}
);
In function success callback when I try to do bookToAssign.setDefault, I get the error: TypeError: Cannot read property 'setDefault' of undefined. Can someone please guide me whats going on here? Appreciate the help
bookToAssignappears to be a javascript global....where is it defined? Also seems strange thatng-modelof select tag has same variable name. My guuess is if you use$scope.bookToAssign.setdefaultwill also run into conflict withng-model. Provide a demong-modeland also likely you never declared$scope.bookAssignas an object in order for it to have any properties likebookAssign