0

I am taking baby steps in learning AngularJS to act as a front end with my Python/Django projects. I just wrote a simple AngularJS page as below.

The expected output of the code is Joe is 35 years old.

while the actual output of the code is {{ person.name }} is {{ person.age }} years old.

The code is as below.

<html>

<body ng-app="scrumboard.demo">
  <div ng-controller="ScrumboardController">
    <p>{{ person.name }} is {{ person.age }} years old.</p>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.10/angular.min.js"></script>

  <script>
    (function() {
        'use strict';
        angular.module('scrumboard.demo', [])
          .controller('ScrumboardController', ['$scope', ScrumboardController]);

        function ScrumboardController($scope) {
          $scope.person = {
            name = 'Joe',
            age = 35
          };
        }());
  </script>

</body>

</html>

3 Answers 3

2

Your object declaration is wrong,

$scope.person = {
   name = 'Joe',
   age = 35
};

name = 'Joe', should be name: 'Joe', and age = 35 should be age: 35

so,

$scope.person = {
   name: 'Joe',
   age: 35
};

<html>

<body ng-app="scrumboard.demo">
  <div ng-controller="ScrumboardController">
    <p>{{ person.name }} is {{ person.age }} years old.</p>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.10/angular.min.js"></script>

  <script>
    (function() {
        'use strict';
        angular.module('scrumboard.demo', [])
          .controller('ScrumboardController', ['$scope', ScrumboardController]);

        function ScrumboardController($scope) {
          $scope.person = {
            name: 'Joe',
            age: 35
          };
        }
     }());
  </script>

</body>

</html>

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

Comments

1

change this

$scope.person = {
        name = 'Joe',
        age = 35
      };

to

$scope.person = {
        name: 'Joe',
        age: 35
      };

<html>

<body ng-app="scrumboard.demo">
  <div ng-controller="ScrumboardController">
    <p>{{ person.name }} is {{ person.age }} years old.</p>
  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.10/angular.min.js"></script>

  <script>
    (function() {
        'use strict';
        angular.module('scrumboard.demo', [])
          .controller('ScrumboardController', ['$scope', ScrumboardController]);

        function ScrumboardController($scope) {
          $scope.person = {
            name: 'Joe',
            age: 35
          };
        }
     }());
  </script>

</body>

</html>

1 Comment

shouldn't self-invoking anonymous function end with })();?
0

Thanks Sravan and Jag. Asa rightly pointed out the problem was with the object notation which I fixed by changing

name = 'Joe',
age = 35

to name: 'Joe', age: 35

However after doing that the code still didn't work. SO after further scrutiny I found out I had missed one curly bracket '}' here

(function() {
    'use strict';
    angular.module('scrumboard.demo', [])
      .controller('ScrumboardController', ['$scope', ScrumboardController]);

    function ScrumboardController($scope) {
      $scope.person = {
        name = 'Joe',
        age = 35
      };
    }());

Which I changed to this

(function() {
    'use strict';
    angular.module('scrumboard.demo', [])
      .controller('ScrumboardController', ['$scope', ScrumboardController]);

    function ScrumboardController($scope) {
      $scope.person = {
        name = 'Joe',
        age = 35
      };
      }
    }());

and it worked.

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.