user2024080 user2024080 - 5 months ago 11
AngularJS Question

why my angular app not working?

i am using the new angularjs

v1.5.7
But I don't know what is wrong with my code, that my app is not running at all.

here is my html file :

<!DOCTYPE html ng-app="myApp">
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Testing App</title>
<link rel="stylesheet" href="">
<script src="angular.js"></script>
</head>
<body ng-controller="main">
<h1>Testing page {{2+2}} {{name}}</h1> //i am not getting any result
<script src="app.js"></script>
</body>
</html>


here is my app.js file :

angular.module('myApp', []);

angular.module('myApp').controller('main', function ($scope) {

alert('hi')

$scope.name = "testing";
});


i am not getting either error or working output here. any one help me here? and correct me?

Answer

Move the ngApp directive onto your html or body tag, it is currently on the doctype declaration.

<!DOCTYPE html >
<html ng-app="myApp">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Testing App</title>
    <link rel="stylesheet" href="">
    <script src="angular.js"></script>
</head>
<body ng-controller="main">
    <h1>Testing page {{2+2}} {{name}}</h1> //i am not getting any result
    <script src="app.js"></script>
</body>
</html>

The directive needs to be on an element, which currently it is not.