Angular指令ng-if不评估条件语句

时间:2014-05-29 15:46:50

标签: javascript html angularjs angular-ng-if

我是web dev和AngularJS的新手。我正在尝试使用指令ng-if只显示div块,如果从数据库返回的列表大于1,但它不起作用。我滥用指令了吗?我环顾四周,找不到任何有效的解决方案。目前,显示两个div并忽略ng-ifs。

<div>
    <div ng-if="listOfThings.length > 1">
        <h1> {{listOfThings.length}} </h1>
        </br>
        <div ng-repeat="thing in listOfThings">
           <label> {{ thing.name }} </label>
        </div>
    </div>
    <div ng-if="listOfThings.length == 1" class="col-sm-4 col-sm-offset-4">
        <h1> {{ listOfThings[0].name }} </h1>
        <iframe width="560" height="315" ng-src="{{ listOfThings[0].embed }}" frameborder="0" allowfullscreen></iframe>
    </div>
</div>

我尝试了这个代码,它在Plunker中运行,但由于某种原因不在我的代码中。在我的代码中,只有ng-app有效,但ng-if仍然没有。

    <div ng-app="ngAnimate">
        Click me: <input type="text" ng-model="check" ng-init="check='show'" /><br/>
        Show when check: {{check}}
        <input ng-if="check!='hide'" class="animate-if" placeholder="type the word 'hide'"/>
    </div>

2 个答案:

答案 0 :(得分:8)

您想要的是ng-if="{{listOfThings.length}} > 1",而不是 ng-if="listOfThings.length>1"

ng-if

{{1}}将评估表达式。

选中 Online Demo

答案 1 :(得分:0)

这应该

<div>
    <div ng-if="listOfThings.length > 1">
        <h1> {{listOfThings.length}} </h1>
        </br>
        <div ng-repeat="thing in listOfThings">
           <label> {{ thing.name }} </label>
        </div>
    </div>
    <div ng-if="listOfThings.length == 1" class="col-sm-4 col-sm-offset-4">
        <h1> {{ listOfThings[0].name }} </h1>
        <iframe width="560" height="315" ng-src="{{listOfThings[0].embed}}" frameborder="0" allowfullscreen></iframe>
    </div>
</div>