2

First, ng-app should be to set the area where angular take place, right? Even if there is ng-controller tag outside ng-app, they should be omitted?

However, when I test on jsfiddle (link here), it seems this is not the case:

HTML

<div ng-app="myApp">
  <div ng-controller="MyCtrl">
    Hello, {{name}}!
    <input ng-model="name">
  </div>
</div><br><br>
<div class="not-inside-my-app">
  <div ng-controller="MyCtrl">
    ...... Some Many other content in real case that I don't want angular to touch .......<br>
    So this is to test angular is not working here.<br>
    Hello, {{name}}!
    <input ng-model="name">
  </div>
</div><br><br>
<div ng-app="myApp">
  <div ng-controller="MyCtrl2">
    Hello, {{name}}!
    <input ng-model="name">
  </div>  
</div>

Javascript

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

myApp.controller('MyCtrl', ['$scope', MyCtrlFunction]);
function MyCtrlFunction($scope) {
    $scope.name = 'Superhero';
}

myApp.controller('MyCtrl2', ['$scope', MyCtrlFunction2]);
function MyCtrlFunction2($scope) {
    $scope.name = 'Non-superhero';
}

The angular code is still working in the middle div. Is my concept on ng-app wrong? Or what have I missed to limit the scope?

P.S. I know I can control the scope by using ng-controller, but it seems to be a waste on resource to do scanning on sections that I know I don't need angularJS.

2
  • 2
    <body ng-app="myApp"> you added in html section under body tag Commented Apr 25, 2017 at 5:39
  • @Durga Good catch!!! This should be closed for typographical error then, yeah? Commented Apr 25, 2017 at 5:40

3 Answers 3

2

Since ng-app attribute added to body tag, which is why all controllers are running.

Since angular considers first ng-app attribute and ignore other ng-app attributes. The above code is working.

NOTE: Though the code written in the html section didn't contain the body tag. You can able to insert the body tag by clicking on the gear icon in the html section of fiddle site. Which is what the main cause for this addition of ng-app attribute added to body tag. Thanks for pointing it out Durga

Screenshot taken in the above fiddle link

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

5 Comments

No need of this much dude, just remove <body ng-app="myApp"> from html body tag
Yes but when you see his jsFiddle link, he didn't add <body> tag. Seems this might be added by some other cause, which is what I mentioned in start. Could be some other issue causing ng-app added to <body> tag
He added, check that html section, under body tag
Yes, you're right. The problem is gone when I use jsbin. jsbin.com/qiqoworebi/edit?html,js,output
Oops you're right. Didn't see that body containing ng-app attribute, which will be seen only by clicking the gear icon on the html section (explanation for those who dont know this like me ;) ) . Will update the answer since its not a fiddle issue.
1

enter image description here

remove body tag <body ng-app="myApp"> from html section, there is no issue with jsfiddle.

2 Comments

Thanks Durga. I came to know about this functionality only today though. Seems not many know this
Thanks! I am editing on top of other's code. Missed the body tag there.
0

It seems some issue with JS jsFiddle, try to run the same example in you local machine it will not work or here you get the answer: https://www.w3schools.com/code/tryit.asp?filename=FEYVPGYHZAY5

1 Comment

You can not call the controller variable out of ng-app

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.