Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm relatively new to Angular and have run into a problem that I can not figure out what I am doing wrong.

Things work up until I get to the edit record screen. From the listing page(this works), controller1 lists out all data. You can click edit on an item to edit. This loads controller2. From there I am not able to access that element in the array. I can see the whole array however.

I am running my data off of a service as well. The app uses ui-routing so $routeParams have been changed to $stateParams.

The code: Service to return data. This part works fine and returns like it should.

.service('attendeeService', function (sfDataProvider) {
    var $attendeeDetailList = [];
    var $strValue = 'test string';
    $attendeeDetailList = sfDataProvider.getAttendeeList();
    return {
        getAttendees:function () {
           return $attendeeDetailList;
        },
        getString:function () {
            return $strValue;
        },
        addAttendee:function (attendeeName) {
        },
        deleteAttendee:function (id) {
        }
    };

Controller1 Lists Array in view. Works fine

.controller( 'AttendeeCtrl', function AttendeeCtrl( $scope, titleService, attendeeService ) {
        titleService.setTitle( 'Attendee Badges' );
        $scope.strValue = attendeeService.getString();
        $scope.attendeeDetailList = attendeeService.getAttendees();
})

Controller2 - I can get the strValue to carry over but cannot access the object fullListAttendees in array to edit item.

.controller( 'AttendeeEditCtrl', function ( $scope, $stateParams, titleService, attendeeService ) {
    var $attendeePerson = [];
    $scope.fullListAttendees =  attendeeService.getAttendees();
    $scope.attendeePerson = $scope.fullListAttendees[$stateParams.id];
    $scope.strValue = attendeeService.getString();
});

Any thoughts or ideas would be greatly appreciated. I don't know if it matters but this was started from the ng-boilerplate for angular.

Thanks for all help

share|improve this question
    
what errors are you getting? –  David Chase Sep 6 '13 at 19:35
    
The error is TypeError: Cannot read property '0' of undefined. It hits on line $scope.attendeePerson = $scope.fullListAttendees[$stateParams.id]; $stateParams.id is the index and comes back as such when Alert to the screen. Therefore I don't believe the array has any data. This is shown to be true in the Angular extention for chrome. No results in array. Not sure why it's not there. –  user2755234 Sep 7 '13 at 21:18
    
I had trouble passing arrays around and ended up copying them. So, instead of returning the array outright in the getter from your service, return array.slice(0), modify the copy of the array, then pass it back in via a setter. Also watch out for inherited scopes, read the section on inherited scopes very carefully, it's tricky. –  aet Sep 9 '13 at 20:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.