0

I wonder why it seems a problem using empty strings for ng-app. In the example provided by w3school, I attempted using empty string to name the ng-app as below, but failed.

<!DOCTYPE html>
<html>
    <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
    </script>
<body>

<p>Try to change the names.</p>

<div ng-app="" ng-controller="myCtrl">

    First Name: <input type="text" ng-model="firstName"><br>
    Last Name: <input type="text" ng-model="lastName"><br>
<br>
    Full Name: {{firstName + " " + lastName}}

</div>

<script>
    var app = angular.module('', []);
    app.controller('myCtrl', function($scope) {
      $scope.firstName= "John";
      $scope.lastName= "Doe";
    });
</script>

</body>
</html>

Even making a " " will make the ng-app directive work. Any ideas?

The reason I was wondering this question is that I learned one example realizing same functionalities without initializing a controller and the ng-app is an empty string.

<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
</script>
<body>

<div ng-app="" ng-init="firstName='John'">

<p>The name is <span ng-bind="firstName"></span></p>

</div>

</body>
</html>
jsh6303
  • 2,010
  • 3
  • 24
  • 50

2 Answers2

1

An empty string null.

But a spaced string is still a string.

Thats why you can use it.

Joe Lloyd
  • 19,471
  • 7
  • 53
  • 81
  • A *spaced string* actually makes it work, as the OP explained. – sp00m Aug 10 '15 at 15:55
  • Thank you. Actually I edited the question and added another example without instantiation of a controller and this time the ng-app is `""`. I realize this is really the reason I thought about the question. I would like to know the reason why we can do it in my second example. – jsh6303 Aug 10 '15 at 15:58
1

you have to set a name to your module, or otherwise not use a module and delete the 'ng-app' from you html.

Isaac
  • 218
  • 1
  • 15
  • Thank you. Actually I edited the question and added another example without instantiation of a controller and this time the ng-app is "". I realize this is really the reason I thought about the question. I would like to know the reason why we can do it in my second example. – jsh6303 Aug 10 '15 at 15:58
  • 1
    my fault! you don`t have to delete the ng-app directive if you dont have a module. "The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application. " [link to ngApp documentation](https://docs.angularjs.org/api/ng/directive/ngApp) . thats why you second example works without a reference to a module. – Isaac Aug 10 '15 at 16:23
  • So if there is no need of further defining module, we can simply leave the ng-app name as `""`? – jsh6303 Aug 10 '15 at 16:28
  • 1
    yes, you can leave the ng-app directive as : ng-app="" or just ng-app – Isaac Aug 10 '15 at 16:31