3

I'm new to angular, I've tried some testing pattern and it's ok with the $scope variable but I can't make it work for a simple controller nesting. (and avoid using the $scope variable, instead I want to use "this")

Here is my sample HTML and javascript :

<!doctype html>
<html ng-app="appTest">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
  </head>
  <body>
    <div ng-controller="FirstController as first">
      <div>
        First is {{first.text}}
      </div>
      <div ng-controller="SecondController as second">
        Second is {{second.text}}
      </div>
    </div>
    <script>
    var app = angular.module("appTest",[]);


    function printFirst() {
      this.text = "first"
    }

    function printSecond() {
      this.text = "second";
    }

    app.controller("FirstController",[printFirst]);
    app.controller("SecondController",[printSecond]);
    </script>
  </body>
</html>

In the output html the angular variables inside curly brackets are not replaced and I don't know what's going on. I've tried to install Angular Batarang for debugging but the scope console is empty.

Obviously it's a silly mistake but I don't see where I'm wrong

2 Answers 2

1

Ok, the answer has nothing to do with my code, I was just using a too old version of Angularjs (1.0.8). I moved to the last version 1.3.4 and it works fine.

Sign up to request clarification or add additional context in comments.

Comments

0

Access the variable using $scope.text please instead of this.text.

3 Comments

I already managed to do that, now I want to use the "as" keyword and avoid the $scope variable
But you are not using it
yes that's what I'm saying, I'm trying not to use the $scope variable in this example, instead I want to use the "as" keyword and "this"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.