2

My Simple Angular JS app isn't working. Can anybody help me out, the following is the code. Index.js is the main file which includes a script file controller.js .

The browser does not shows any errors when I try to run the app:

var app = angular.module('myApp',[]);
app.controller('myCtrl',function($scope){
    $scope.name="Angad";
});
<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <meta charset="utf-8" />
    <title>Welcome</title>
</head>
<body ng-controller="myCtrl">
    <nav>Navigation</nav>
    <div ng-controller="myCtrl">
        <input type="text" ng-model="name">
        <h1>{{name}}</h1>
    </div>
    <footer>Copyright-2017</footer>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</body>
</html>

11
  • why you declare 2 controllers ? Plus make sure angular.min.js has loaded properly Commented Jan 13, 2017 at 11:21
  • 1
    Ok remove ng-controller="myCtrl" from body tag Commented Jan 13, 2017 at 11:22
  • 1
    your code is working fine. How are you testing your app ? Simply by opening index.html in a browser ? Commented Jan 13, 2017 at 11:23
  • @Weedoze ... Yes by running the file index.html in the browser Commented Jan 13, 2017 at 11:26
  • @AngadbirSinghSandhu That's not how to run an AngularJS app. You should launch via a servr Commented Jan 13, 2017 at 11:27

2 Answers 2

6

it is due to script loading statement

change

<script src="js/angular.min.js" />
<script src="js/controller.js" />

with

<script src="js/angular.min.js"></script>
<script src="js/controller.js"></script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks .. I worked :) But I do want to know why it didnt worked the other way around ... I mean whats the diff between <script /> and <script></script> in this context
<Script> tag is not self closing tag like <input> tag
2

Simple Angularjs app works without a webserver. Your example will also work.

Just close the script tag properly.

<script src="js/angular.min.js"></script>
<script src="js/controller.js"></script>

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.