Angularjs controller socket

I'm new to angular, I've tried some test template and it's ok with the $ scope variable, but I can't seem to get it to work for simple controller nesting. (and don't use the $ scope variable, I want to use "this" instead)

Here's my HTML and javascript example:

<!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 html output, angular variables inside curly braces are not replaced and I have no idea what is going on. I tried to install angular Batarang for debugging, but the scope console is empty.

Obviously this is a stupid mistake, but I can't see where I am going wrong

+3


source to share


2 answers


Ok, the answer has nothing to do with my code, I just used too old version of Angularjs (1.0.8). I upgraded to the latest version 1.3.4 and it works great.



+1


source


Access the variable using $scope.text

instead this.text

.



0


source







All Articles