batuman batuman - 1 month ago 8
AngularJS Question

View doesn't display correct model data from controller in AngularJS

I am testing AngularJS as follow.

Script.js

var myApp = angular.module("myModule",[]);


myApp.controller("myController", function ($scope){
var employee = {
firstName = "Test",
lastName = "Name",
gender = "Male"
};
$scope.employee = employee;
});


index.html

<html ng-app="myModule">
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="Scripts/angular.min.js" type="text/javascript"></script>
<script src="Scripts/Script.js" type="text/javascript"></script>
</head>
<body ng-controller="myController">
<div>
{{ employee.firstName }}
</div>
<div>
{{ employee.lastName }}
</div>
<div>
{{ employee.gender }}
</div>

</body>
</html>


When I run the program, I just see as

{{ employee.firstName }}
{{ employee.lastName }}
{{ employee.gender }}


I don't see in correct format.
What could be wrong?

Thanks

Answer

There is an issue in the way you are assigning values to the employee object , it should be like this,

myApp.controller("myController", function ($scope){
    var employee = {
        firstName : "Test",
        lastName : "Name",
        gender : "Male"        
    };
    $scope.employee = employee;    
});

DEMO