为什么我的ng-if无法正常工作?它没有隐藏div [英] Why is my ng-if not working? It's not hiding the div
本文介绍了为什么我的ng-if无法正常工作?它没有隐藏div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Angular应用程序中,我希望某个div
在变量为true时显示,而在变量为false时消失.
In my Angular app, I want a certain div
to appear if a variable is true, and to disappear if it is false.
但是,它不起作用. 看我的小提琴
However, it is not working. See my Fiddle
有人可以帮助我理解为什么吗?
Can anyone help me understand why?
HTML
<div ng-controller="MyCtrl">
<div id="newProjectButton" class="project" ng-click="newProjectActive()" ng-if="!creatingNew">
<h1> + </h1>
<h3> New Project </h3>
</div>
<div id="newProjectActive" class="project" ng-if="creatingNew">
<form>
<input name="name" ng-model="newProjectName" type="text"></input>
<button ng-click="newProject()" type="submit" class='btn btn-primary'>Save</button>
</form>
</div>
</div>
JS
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.name = 'Superhero';
$scope.creatingNew = false;
$scope.newProjectActive = function () {
$scope.creatingNew = true;
}
$scope.newProject = function () {
alert($scope.newProjectName);
}
}
推荐答案
首先,当我看着您的小提琴时,那里有您示例的旧版本,
First, when I looked at your fiddle, there was older version of your example there,
第二个,实际上可能是一个原因,在该示例中,您在1.0.1版中使用了angular,我相信该版本未实现ng-if.更新到最新版本将解决您的问题
Second, which actually may be a reason, is in that example you were using angular in version 1.0.1, and i believe that version didn't implement ng-if. Updating to latest version will fix your problem
这篇关于为什么我的ng-if无法正常工作?它没有隐藏div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文