I have an EventController and use EventService for saving to localStorage.
vm.event = event;
function event() {
vm.dataLoading = true;
EventService.Create(vm.events) //save using ( api/events/ + id )
.then(function (response) {
if (response.success) {
FlashService.Success('Event created successful', true);
$location.path('/events');
} else {
FlashService.Error(response.message);
vm.dataLoading = false;
}
});
}
View All Events:
<td>{{events.eventType}} </td>
<td>{{events.eventName}}</td>
...
My problem is I tried adding a guest list with an array
vm.guests = [];
vm.addGuest = function () {
vm.guests.push(vm.newGuest);
}
vm.removeGuest = function (guest) {
var index = vm.guests.indexOf(guest);
vm.guests.splice(index, 1);
}
html
<input type="text" name="guests" id="guests" class="form-control" ng-model="vm.newGuest" />
<button type="submit" ng-click="vm.addGuest()" class="btn btn-primary">Add Guest</button>
<div ng-repeat="guest in vm.guests track by $index">
{{guest}}
I can add strings to the array from a single input/button
However, the array doesn't get passed to the event object.
{
"eventType": "Birthday",
"eventName": "Part at Lake Lettuce"
}
[
"Joe",
"Tom"
]
My goal is to add the array to the object
{
"eventType": "Birthday",
"eventName": "Part at Lake Lettuce"
"guests": [
"Joe",
"Tom"
]
}
vm.guests = []. you should avoid this and usevm.guests.length = 0this will empty the array but maintain the data bindingvm.guests.length = 0errors sayingCannot set property 'length' of undefined at new EventController<td>{{events.eventType}} </td>be<td>{{vm.events.eventType}} </td>?