0

I am having an issue where my controller variables are not being initialized correctly as per the controller code.

The controller code is

  angular.module('test')
  .controller('RegisterCtrl', function RegisterCtrl($scope) {
var vm = this;
   vm.model = {companyName:'aaa'};
    vm.data= [{fieldtype: 'input',key:'companyName'}]; 
});

The ui-router is

  .state('register', {
        //     abstract: true,
        url: "/register",
        templateUrl: "/views/register.html",
        controller: 'RegisterCtrl',
        controlleras: 'vm'
      })

I have a verify basic html to show the output of model:

<h2>Model</h2>
<pre>{{vm.model }}</pre>

When debugging the code in chrome it passes through the controller code and sets both model and data correctly, but when the page has finished loading the vm.model is blank.I am not sure what I am doing incorrectly.

Thanks in advance.

4
  • The state configuration object for ui-router does not have a controlleras property. You need to specify it in your controller property instead (i.e. controller: 'RegisterCtrl as vm'). Commented Dec 3, 2015 at 0:42
  • 2
    @miqid actually, it does, but it's case sensitive. it is controllerAs. This is a simple typo. Commented Dec 3, 2015 at 0:59
  • @Claies, my bad! Indeed it does. Only two table rows down in the documentation too. :-( Commented Dec 3, 2015 at 1:02
  • Yes it was case sensitive and now works.....thanks Commented Dec 3, 2015 at 2:50

2 Answers 2

0

Try using this as your HTML body tag:

<body ng-controller="RegisterCtrl as vm" ng-app="test">
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of var, I would advice you to use $scope.model, for this purpose

angular.module('test')
 .controller('RegisterCtrl',['$scope',function($scope) {
   $scope.model = {
                    companyName:'aaa'
                  };
   $scope.data= [
                 {
                   fieldtype: 'input',
                   key:'companyName'
                 }
                ]; 
 }]);

HTML

<h2>Model</h2>
<pre ng-bind="model.companyName"></pre>
<pre>{{model.companyName}}</pre>
<pre>{{model}}</pre>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.