Search code examples
javascripthtmlangularjssharepoint-2010scope

Angular ng-change event not working on first change?


Still learning the ropes with Angular and I have a form that loads students into a select dropdown from a JSON return and I want to change school name label to the school listed as the students school based on the student id. This all part of the JSON and it's working...sort of. When I select the student from the dropdown nothing happens but if I change selection back to the default the change works and label now has the intended value.

in my .ascx

<select name="studentList" ng-model="student" ng-change="getStudent(student)" ng-options="s.StudentID as s.FirstName for s in cStudents | orderBy:'FirstName'">
                <option value="">Choose Students...</option>
            </select>

from the controller

angularVolunteerApp.controller('vAppController',
function vAppController($scope, $window) {
    $scope.cStudents = $window.xStudents;

    $scope.getStudent = function(xStudent) {

        angular.forEach(xStudents, function (s) {

            if (s.StudentId == xStudent) {
                $scope.cSchoolName = s.SchoolName;
            }
        });

        //$scope.cSchoolName = xStudent;
    };

});

Not sure if this is relevant but this is for a webpart in Sharepoint 2010


Solution

  • Found your main problem, null can't be in caps:

    "CurrentStatus":null 
    

    NOT

    "CurrentStatus":NULL
    

    Add that in with the inconsistent naming of variables (casing was not consistent) and you're in for a wild ride!

    I fixed it all up for you and made it do what I think you want it to do, check it out here:

    http://plnkr.co/edit/o5Y12VUrNX0OHNxJ3dT5?p=preview