javascript - 使用空字符串 ("") 作为 ng-app 的名称

标签 javascript angularjs angularjs-directive

我想知道为什么 ng-app 使用空字符串似乎有问题。 在 example由w3school提供,我尝试使用空字符串来命名ng-app,如下所示,但失败了。

<!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>

即使创建 "" 也会使 ng-app 指令起作用。有什么想法吗?

我想知道这个问题的原因是我学到了一个在不初始化 Controller 的情况下实现相同功能的示例,并且 ng-app 是一个空字符串。

<!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>

最佳答案

您必须为模块设置名称,否则不使用模块并从 html 中删除“ng-app”。

关于javascript - 使用空字符串 ("") 作为 ng-app 的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31923949/

相关文章:

javascript - 在 JavaScript 中尝试使用 localStorage 保存和加载包含对象的数组时出错

javascript - AngularJS:提交时重置表单

javascript - 在 Angularjs 中,嵌套在指令中的 Controller 可以设置指令的 ng-model 吗?

javascript - 编译模板时的代码味道

javascript - 类型错误 : Attempted to wrap undefined property find as function - NodeJs

javascript - 遍历 firebase 存储以显示多个图像

javascript - 从 PhoneGap JavaScript 访问 Android 上的 Content Provider

javascript - 双向绑定(bind)不适用于 ng-repeat

javascript - 在 AngularJs 中绑定(bind)标签名称

angularjs - Angular 指令 - 当另一个指令也是隔离范围时更新父范围