R
R
Roman Rakzin2015-07-28 16:38:03
Angular
Roman Rakzin, 2015-07-28 16:38:03

ng-options and ng-model in angularjs?

There is markup

{{SelectedSemester}} -тут всё хорошо отображается
    <select ng-model="SelectedSemester" ng-change="" ng-options="semester.Id as semester.Name for semester in DataService.GET_SemestersList()">
    <button ng-click="group_create_show()">показать</button>

and code
$scope.group_create_show= function() { 
      alert($scope.SelectedSemester) // а здесь не выводится пробовал $scope.SelectedSemester.Id
    }

DataService.GET_SemestersList() returns a list of semesters
Revised all documentation. It works everywhere, but not here. Weird.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-07-28
@TwoRS

This is the most common Angular beginner mistake. Scopes cannot inherit scalars. Something a little higher in the hierarchy most likely created its own scope, and its ancestor is in the controller.
There are two solutions:
- use storage objects ala data. SelectedSemester
- don't use the scope in the controller and use the controllerAs syntax, then everything will be fine since the controller will be scoped by alias and since the controller is an object passed by reference, the values ​​will be inherited.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question