AngularJS: Initialize nested scope variables in controller

Tyler

I like to keep my namespace of model names clean and descriptive, so I use nested model variables like this (where month and year are nested in service):

<form data-ng-submit="submit()">
  <select data-ng-model="service.month" data-ng-options="m for m in months"/>
  <input data-ng-model="service.year"/> {{ service.year }}
  <input type="submit"/>
</form>

And in my controller, I want to set $scope.service.month and $scope.service.year to initial values, but I get a javascript error Cannot set property 'year' of undefined, so I'm guessing the DOM hasn't been parsed and all the $scope variables haven't been created yet. How can I have some code wait to run until the DOM has been parsed by Angular and all the models have been created?

Here's my controller:

mod.controller('AddServiceCtrl', [
    '$scope', 
    '$rootScope', 
function($scope, $rootScope) {
  var date = new Date();
  $scope.months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  $scope.service.year = 2014;   
  $scope.submit = function(){
  };
}])
mpm
 $scope.service= {month:'Mar',year:2014};

AngularJS is still JavaScript.

The AngularJS team unfortunately chose to name what is actually a presenter (or a view model), a controller. It might not sound that important, but conceptually it is in order to understand what controllers do.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Initialize $scope variables for multiple controllers - AngularJS

From Dev

How do I initialize a nested controller in AngularJS?

From Dev

AngularJS set ng-controller with scope variables

From Dev

AngularJS directive won't detect controller scope that binds variables using "this"

From Dev

Passing scope variables to an AngularJS controller using fat arrows

From Dev

Scope variables undefined in a controller

From Dev

Angularjs "Controller as" or "$scope"

From Dev

Angularjs - Passing the $scope to a controller

From Dev

understanding the controller scope in angularJS

From Dev

AngularJS: How to initialize a $scope attribute

From Dev

Is AngularJS controller performance impacted by having many $scope declarations as opposed to a nested $scope object?

From Dev

AngularJs scope variables in console

From Dev

Testing AngularJS scope variables

From Dev

AngularJS - Initialize services before controller

From Dev

AngularJS: initialize controller with different services

From Dev

AngularJS: Initialize a controller for each directive

From Dev

AngularJS Controller not working in nested Controller

From Dev

Controller in Nested views of AngularJS

From Dev

Controller in Nested views of AngularJS

From Dev

How to initialize form as scope variable in controller

From Dev

Altering $scope controller variables on URL

From Dev

Angular $scope variables not updating in controller

From Dev

declaring variables in angularjs controller

From Dev

declaring variables in angularjs controller

From Dev

AngularJS initializing $scope of directive/controller

From Dev

AngularJS — injecting scope (without $) into a controller

From Dev

Alias the AngularJS $scope object in a controller

From Dev

Binding directive scope with controller in AngularJS

From Dev

AngularJS Isolated Scope and Controller Binding

Related Related

  1. 1

    Initialize $scope variables for multiple controllers - AngularJS

  2. 2

    How do I initialize a nested controller in AngularJS?

  3. 3

    AngularJS set ng-controller with scope variables

  4. 4

    AngularJS directive won't detect controller scope that binds variables using "this"

  5. 5

    Passing scope variables to an AngularJS controller using fat arrows

  6. 6

    Scope variables undefined in a controller

  7. 7

    Angularjs "Controller as" or "$scope"

  8. 8

    Angularjs - Passing the $scope to a controller

  9. 9

    understanding the controller scope in angularJS

  10. 10

    AngularJS: How to initialize a $scope attribute

  11. 11

    Is AngularJS controller performance impacted by having many $scope declarations as opposed to a nested $scope object?

  12. 12

    AngularJs scope variables in console

  13. 13

    Testing AngularJS scope variables

  14. 14

    AngularJS - Initialize services before controller

  15. 15

    AngularJS: initialize controller with different services

  16. 16

    AngularJS: Initialize a controller for each directive

  17. 17

    AngularJS Controller not working in nested Controller

  18. 18

    Controller in Nested views of AngularJS

  19. 19

    Controller in Nested views of AngularJS

  20. 20

    How to initialize form as scope variable in controller

  21. 21

    Altering $scope controller variables on URL

  22. 22

    Angular $scope variables not updating in controller

  23. 23

    declaring variables in angularjs controller

  24. 24

    declaring variables in angularjs controller

  25. 25

    AngularJS initializing $scope of directive/controller

  26. 26

    AngularJS — injecting scope (without $) into a controller

  27. 27

    Alias the AngularJS $scope object in a controller

  28. 28

    Binding directive scope with controller in AngularJS

  29. 29

    AngularJS Isolated Scope and Controller Binding

HotTag

Archive