使用空字符串(“")作为 ng-app 的名称 [英] Using empty string ("") as name for ng-app

查看:30
本文介绍了使用空字符串(“")作为 ng-app 的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么在 ng-app 中使用空字符串会出现问题.在 w3school 提供的 example 中,我尝试使用空字符串来命名ng-app 如下,但失败了.

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"><身体><p>尝试更改名称.</p><div ng-app="" ng-controller="myCtrl">名字:<input type="text" ng-model="firstName"><br>姓氏:<input type="text" ng-model="lastName"><br><br>全名:{{firstName + " " + lastName}}

<脚本>var app = angular.module('', []);app.controller('myCtrl', function($scope) {$scope.firstName="约翰";$scope.lastName="母鹿";});</html>

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

我想知道这个问题的原因是我学习了一个示例,无需初始化控制器即可实现相同的功能,并且 ng-app 是一个空字符串.

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"><身体><div ng-app="" ng-init="firstName='John'"><p>名字是<span ng-bind="firstName"></span></p>

</html>

解决方案

你必须为你的模块设置一个名称,否则不使用模块并从你的 html 中删除ng-app".

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>

解决方案

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

这篇关于使用空字符串(“")作为 ng-app 的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆