【问题标题】:ng-if (Angularjs) comparison between two objectsng-if (Angularjs) 比较两个对象
【发布时间】:2015-08-14 05:19:31
【问题描述】:

我从一个控制器移动了几个编队,我想使用“ng if”来比较id,但它不起作用。

代码如下:

    var postsApi = 'http://mywebsite.com/wp-json/posts?filter[posts_per_page]=4&_jsonp=JSON_CALLBACK';


  $http.jsonp( postsApi ).
    success(function (data, status, headers, config) {

        $scope.posts = data;

      console.log( data );
    }).
    error(function(data, status, headers, config) {
      console.log( 'Post load error.' );
    });

  var newsApi = 'http://mywebsite.com/wp-json/posts?filter[category_name]=news&filter[posts_per_page]=4&_jsonp=JSON_CALLBACK';

    // This should go in a service so we can reuse it
  $http.jsonp(newsApi).
    success(function (data, status, headers, config) {

        $scope.news = data;

        console.log(data);
    });

  var jworldApi = 'http://mywebsite.com/wp-json/posts?filter[category_name]=world&filter[posts_per_page]=4&_jsonp=JSON_CALLBACK';

    // This should go in a service so we can reuse it
    $http.jsonp(jworldApi).
    success(function (data, status, headers, config) {

        $scope.jworld = data;

        console.log(data);
    });

在视图中:

    <div class="highlight-topright">


                    <ion-item class="main_post" ng-repeat="post in posts" ng-if="$index == 0" href="#/kikarnews/posts/{{post.ID}}">

                        <div class="wrapper">

                            <img class="full-image" src="{{ post.featured_image.attachment_meta.sizes.medium.url }}" />

                            <h3 class="cat_name_main"> {{post.terms.category[0].name}}</h3>

                            </div>

                            <h2 ng-bind-html="post.title"></h2>




</ion-item>

            <ion-item ng-repeat="post in posts" ng-if="$index != 0"     href="#/mysite/posts/{{post.ID}}">

                 <div class="row main_home">


                     <div class="col col-50 main_img_home">
                         <img class="full-image" src="{{ post.featured_image.attachment_meta.sizes.medium.url }}">
                     </div>


                     <div class="col col-50 main_title_home">
                         <h5>{{ post.date }}</h5>


                         <h2 ng-bind-html="post.title"></h2>



                         </div>

                     </div>



</ion-item>
</div>

        <!---news-->

        <div class="news_mideel_home">

            <h2>news</h2>

            <ion-item ng-repeat="newsi in news" **ng-if="newsi.ID != post.ID**" href="#/mysite/posts/{{newsi.ID}}">

                <div class="row main_home">


                    <div class="col col-50 main_img_home">
                        <img class="full-image" src="{{ newsi.featured_image.attachment_meta.sizes.medium.url }}">
                    </div>


                    <div class="col col-50 main_title_home">
                        <h5>{{ newsi.date }}</h5>


                        <h2 ng-bind-html="newsi.title"></h2>



                    </div>

                </div>

            </ion-item>


        </div>



        <div class="jworld">

           <h2>jworld</h2>

            <div ng-repeat="jworldi in jworld">

                <ion-item ng-if="jworldi.ID == 5358"      href="#/mysite/posts/{{jworldi.ID}}">

                    <div class="row main_home">


                        <div class="col col-50 main_img_home">
                            <img class="full-image" src="{{jworldi.featured_image.attachment_meta.sizes.medium.url }}">
                        </div>


                        <div class="col col-50 main_title_home">
                            <h5>{{ jworldi.date }}</h5>


                            <h2 ng-bind-html="jworldi.title"></h2>



                        </div>

                    </div>

                </ion-item>


        </div>

            </div>

【问题讨论】:

  • 尝试创建一个plunker / fiddle而不是让你的问题变得很大。
  • 代码的输出是什么?

标签: javascript jquery json angularjs wordpress


【解决方案1】:

如果您对新闻 div 中的以下行有疑问

ng-if="newsi.ID != post.ID 它按预期工作,因为帖子范围以第一个 div 标签结束。所以在 news_mideel_home 中帖子是空的。但是你可以通过在 i 周围添加一个新的 div 标签来添加它,如下所示

<div ng-repeat="post in posts">
<ion-tem ng-repeat="newsi in news" **ng-if="newsi.ID != post.ID**" href="#/mysite/posts/{{newsi.ID}}">>
------
</ion-item>
</div>

【讨论】:

  • 谢谢。它仅在 == ng 时有效,但在 -! =。知道为什么吗?
  • 你能粘贴你的 div 标签和 if 条件吗?
  • 它不起作用意味着它没有出现?意味着它可能永远是平等的?
  • 我在页面顶部有三个项目。下面我展示了更多的项目a,但我不希望Fawcett 出现了也会被down。所以我比较了id,以确保他不一样。
猜你喜欢
相关资源
最近更新 更多
热门标签