M.Hasham M.Hasham - 3 months ago 14
JSON Question

Simple JS/JSON not working with Angular

I'm starting to learn Angular JS, and I wrote the following code to display name, age and profession. I'm using Angular 1.5.



<!-- View -->

<div ng-controller="MyController">
<h1> {{author.name}} </h1>
<p>{{author.age + " " + author.profession}}</p>
</div>


<!-- MODAL -->
<script>
function MyController($scope) {
$scope.author = {
'name': 'Muhammad',
'age': '35',
'profession': 'Software Engineer'
}
}
</script>





Any help would be much appreciated.

Answer

You need to add a module:

var app = angular.module("main", []); 
app.controller("MyController", MyController);

<html ng-app="main">
<div ng-controller="MyController">
  <h1> {{author.name}} </h1>
  <p>{{author.age + " " + author.profession}}</p>
</div>

</html>

working example: http://codepen.io/nilestanner/pen/YWmWNo

Comments