使用空字符串(“")作为 ng

编程入门 行业动态 更新时间:2024-10-19 14:40:40
本文介绍了使用空字符串(“")作为 ng-app 的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

<script src="http://ajax.googleapis/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/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/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/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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 22:24:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1011306.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空字符串   quot   ng

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!