简体   繁体   English

AngularJS ng-if不起作用

[英]Angularjs ng-if is not working

Appreciate if anyone could help explain why my checkbox Voted: is not able to control the ng-if directive? 赞赏是否有人可以帮助解释为什么我的复选框Voted:无法控制ng-if指令? However, it is workable for the Keep HTML: check box 但是,它对于保留HTML:复选框是可行的。

<p>
  <li ng-repeat="x in info">
    {{x.name}} Voted:
    <input type="checkbox"
      ng-model="myVar"
      ng-init="myVar =true">
  </li>
</p>

<input type="checkbox" ng-model="myVar" ng-init="myVar =true">
<h1 ng-if="myVar">Welcome2</h1>
<script>
  var app = angular.module("myApp", []);
  app.controller("myCtrl", function($scope) {
    $scope.myObj = {
      "color": "white",
      "background-color": "coral",
      "font-size": "60px",
      "padding": "50px"
    }

    $scope.info = [{
      name: 'woon',
      age: '18'
    },
    {
      name: 'amir',
      age: '17'
    }];
  });
</script>

You need to use ng-show. 您需要使用ng-show。 Working example: 工作示例:

ng-show="myVar"

http://jsfiddle.net/f8cjv7eL/ http://jsfiddle.net/f8cjv7eL/

what is the difference between ng-if and ng-show/ng-hide ng-if和ng-show / ng-hide有什么区别

You shouldn't use template variables for that, use scope.myVar that way you can change it any time depending on , by the way, myVar is out of loop, you have 2 options 您不应该为此使用模板变量,而应使用scope.myVar这样,您可以随时更改它,具体取决于,顺便说一下,myVar处于循环状态,您有2个选择

1) extract that value in JS part (only if the value is being calculated) 2) move that input into the loop (if you need to display an input for every record on the array) 1)在JS部分中提取该值(仅在计算该值时)2)将输入移入循环(如果需要为数组中的每个记录显示一个输入)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM