AngularJs ng-show用法

时间:2016-07-30 09:42:08

标签: angularjs angularjs-ng-show

我是AngularJs的新手,我只想在复选框值为true时看到标题。我将其初始值设置为false,但标头在首次运行时显示。我的问题是为什么标题首次显示没有任何点击?



var app = angular.module("myApp", []);
app.controller("myController", function($scope) {
  $scope.myVar = "false";
});

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myController">
  <form>
    <input type="checkbox" ng-model="myVar">if u want to see header click this checkbox</input>
    <h1 ng-show="myVar">show this if checkbox returned true</h1>
    </br>
    {{myVar}}
  </form>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

您正在为myVar分配一个字符串,从而将其强制转换为布尔值,它始终为true。将其分配给实际布尔值:$scope.myVar = false

答案 1 :(得分:0)

因为你赋值是一个字符串。只提供一个工作正常的布尔值。

<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>  
    <div ng-app="myApp" ng-controller="myController">
        <form>
            <input type="checkbox" ng-model="myVar">if u want to see header click this checkbox</input>
            <h1 ng-show="myVar">show this if checkbox returned true</h1></br>
            {{myVar}}
        </form>
    </div>
<script>
    var app=angular.module("myApp",[]);
    app.controller("myController",function($scope){
        $scope.myVar=false;
    });
</script>
</body>
</html>

答案 2 :(得分:0)

从$ scope.myVar删除引号=&#34; false&#34 ;; 在这里,您要为$ scope.myVar分配一个字符串值,因此当您第一次加载页面时它不起作用,因为它没有获得所需的布尔值。

相关问题