Search code examples
angularjsangular-ngmodel

angularjs ng-model not able to access in controller


I can't able to access this ng-model name viewProfileCtrl.monthsForm.institution in Controller

<select class="perf-select" ng-model="viewProfileCtrl.monthsForm.institution"
                           ng-options="inst.institution.institution_id as inst.institution.name for inst in viewProfileCtrl.perfiosAnalysisData.institutions"
                           ng-change="viewProfileCtrl.setCurrMonthInsti(viewProfileCtrl.monthsForm.institution)">
                           <option value=""  selected>Select a Bank</option>
                        </select>

if i try to access it show error like this TypeError: Cannot set property 'institution' of undefined

How i can access this ?

myjs

vm.monthsForm.institution = vm.perfiosAnalysisData.institutions[0].institution.institution_id;

Solution

  • In your js, you should define monthsFrom before assigning institution property in it.

    You can do like this :

    vm.monthsForm = {
            'institution' : vm.perfiosAnalysisData.institutions[0].institution.institution_id;
    };