使用名称禁用变量初始化初始化ng-app

时间:2015-03-12 18:34:56

标签: javascript angularjs angularjs-directive angularjs-controller

以下:

<div ng-app="" ng-init="show_login=false;count=0">
<button> ng-click="show_login=!show_login;count=count+1">INSPIRE</button>
<form ng-show= "show_login">
     Username <input  type ="text"> <br>
     Password <input type = "password">
      <p> {{count}} </p> 
</form>
</div>

我的表单工作正常,但是当我为ng-app模块指定名称时,表单无效。

<div ng-app="demo" ng-init="show_login=false;count=0">
<button> ng-click="show_login=!show_login;count=count+1">INSPIRE</button>
<form ng-show="show_login">
     Username <input  type ="text"> <br>
     Password <input type = "password">
      <p> {{count}} </p> 
</form>
</div>

这是否意味着如果我给出一个模块名称,我必须通过控制器进行所有操作和变量初始化,否则我的解释是错误的?

1 个答案:

答案 0 :(得分:1)

以下适用于我。如果你像我下面那样设置模块,它应该可以工作

angular.module("demo", []);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demo" ng-init="show_login=false;count=0">
<button ng-click="show_login=!show_login;count=count+1">INSPIRE</button>
<form ng-show= "show_login">
     Username <input  type ="text"> 
     <br />
     Password <input type = "password">
      <p> {{count}} </p> 
</form>
</div>