无法使用ng-if和ng-repeat

时间:2014-05-01 18:07:45

标签: angularjs angular-ng-if

我尝试使用ng-if在ng-repeat中,但似乎ng-if无法正常工作。

我在StackOverflow中引用了几个论坛链接,但它对我没有帮助。

我正在使用角度版本1.3.0

HTML

<!DOCTYPE html>
<html ng-app="myModule">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <script src="/js/lib/angular-1.3.0/angular.js"></script>
        <script src="/js/lib/controllers/ifRepeatController.js"></script>
    </head>
    <body>

     <div ng-repeat = "data in comments">
      <div ng-if="data.type == 'hootsslllll' ">
          //differnt template with hoot data
       </div>
      <div ng-if="data.type == 'story' ">
          //differnt template with story data
       </div>
       <div ng-if="data.type == 'article' ">
          //differnt template with article data
       </div> 
     </div>
    </body>
</html>

控制器

var myIfRepeat = angular.module('myIfRepeat',[]);


myIfRepeat.controller('IfRepeatController', function ($scope,$http) {

     $scope.comments = [
            {"_id":"1",
               "post_id":"1",
               "user_id":"UserId1",
               "type":"hoot"},  
            {"_id":"2",
               "post_id":"2",
               "user_id":"UserId2",
               "type":"story"},        
            {"_id":"3",
               "post_id":"3",
               "user_id":"UserId3",
               "type":"article"}
          ];

});

根据第一个条件,第一行不应该显示(下面找到屏幕截图以获得更多参考),但是显示了这一行。

参考链接:Using ng-if inside ng-repeat?

Ng-If

现在我按照建议在div中添加了控制器,但是我面临同样的问题

下面我给出了截图供参考。请帮助我解决这个问题,如有进一步澄清,请告诉我。

New screenshot with controller

下面提供的工作模型截图使用了angular 1.3版本。如果我们使用角度1.0.2,则nf-if将无法正常工作(下面提供了屏幕截图)

Working model screenshot

角度版本1.0.2的屏幕截图

enter image description here

4 个答案:

答案 0 :(得分:4)

我对此工作没有任何问题。调整您的代码以适合fiddle

<div ng-app ng-controller="IfRepeatController">
    <div ng-repeat="data in comments">
        <div ng-if="data.type == 'hootsslllll' ">type={{data.type}}//differnt template with hoot data</div>
        <div ng-if="data.type == 'story' ">type={{data.type}}//differnt template with story data</div>
        <div ng-if="data.type == 'article' ">type={{data.type}}//differnt template with article data</div>
    </div>
</div>

function IfRepeatController($scope) {
    $scope.comments = [{
        "_id": "1",
            "post_id": "1",
            "user_id": "UserId1",
            "type": "hoot"
    }, {
        "_id": "2",
            "post_id": "2",
            "user_id": "UserId2",
            "type": "story"
    }, {
        "_id": "3",
            "post_id": "3",
            "user_id": "UserId3",
            "type": "article"
    }];
}

注意:参考角度1.3.0:https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.7/angular.min.js

答案 1 :(得分:2)

您遗失ng-controller

<body ng-controller="IfRepeatController">

答案 2 :(得分:0)

我有类似的问题。我无法得到 - 如果能正常工作。让它工作的唯一方法是评估ng-if到not运算符(ng-if =“!variableValue”)。这是一个AngularJS版本问题吗?

答案 3 :(得分:0)

这不是 AngularJs 版本问题,如果variableValue有布尔值,它将正常工作。

示例代码如下:

$scope.variableValue = true; in Controller

"<"div ng-if="!variableValue" ">"Don't show content"<"/div">"// This DIV doesn't show the content 
"<"div ng-if="variableValue" ">"show content"<"/div">"  // This DIV will show the content