Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用空字符串(";)作为ng应用程序的名称_Javascript_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript 使用空字符串(";)作为ng应用程序的名称

Javascript 使用空字符串(";)作为ng应用程序的名称,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我想知道为什么为ng应用程序使用空字符串似乎是个问题。 在w3school提供的中,我尝试使用空字符串为ng应用程序命名,如下所示,但失败 <!DOCTYPE html> <html> <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script> <body> <p>Try to

我想知道为什么为
ng应用程序
使用空字符串似乎是个问题。
在w3school提供的中,我尝试使用空字符串为ng应用程序命名,如下所示,但失败

<!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>
空字符串为null

但间隔的字符串仍然是字符串


这就是为什么你可以使用它

您必须为模块设置名称,否则就不能使用模块并从html中删除“ng应用程序”。

为什么要这样做?@sma我做了编辑,添加了另一个示例。这让我想到了为什么我们不能这样做。正如OP解释的,一个间隔的字符串实际上可以让它工作。谢谢。实际上,我编辑了这个问题并添加了另一个没有实例化控制器的示例,这次ng应用程序是
。我意识到这就是我思考这个问题的原因。我想知道为什么我们可以在我的第二个例子中这样做。谢谢。实际上,我编辑了这个问题并添加了另一个没有实例化控制器的示例,这次ng应用程序是“”。我意识到这就是我思考这个问题的原因。我想知道为什么我们可以在我的第二个例子中这样做。我的错!如果没有模块,则不必删除ng app指令。“文档中找到的第一个ngApp将用于将根元素定义为应用程序自动引导。”。这就是为什么第二个示例在不引用模块的情况下工作。因此,如果不需要进一步定义模块,我们可以简单地将ng应用程序名称保留为
?是的,您可以将ng应用程序指令保留为:ng app=“”,或仅保留ng应用程序
<!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>