使用 ng-if 作为 ng-repeat 中的开关? [英] Using ng-if as a switch inside ng-repeat?

查看:28
本文介绍了使用 ng-if 作为 ng-repeat 中的开关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Angular 应用.我尝试使用 ng-if 并在 ng-repeat 内切换但没有成功.我有如下数据:

 **[{"_id":"52fb84fac6b93c152d8b4569","post_id":"52fb84fac6b93c152d8b4567","user_id":"52df9ab5c6b93c8e2a8b4567","type":"hoot",},{"_id":"52fb798cc6b93c74298b4568","post_id":"52fb798cc6b93c74298b4567","user_id":"52df9ab5c6b93c8e2a8b4567","类型":"故事",},{"_id":"52fb7977c6b93c5c2c8b456b","post_id":"52fb7977c6b93c5c2c8b456a","user_id":"52df9ab5c6b93c8e2a8b4567","类型":"文章",},**

$scope.comments = 上面提到的数据
和我的 Html 一样:

 

<div ng-if="hoot == data.type">//不同的模板与热门数据

<div ng-if="story == data.type">//具有故事数据的不同模板

<div ng-if="article == data.type">//带有文章数据的不同模板

我怎样才能在 Angular 中实现这件事?

解决方案

尝试包围 strings (hoot, story, article) 带引号 ':

<div ng-if="data.type == 'hoot'">//带有热门数据的不同模板

<div ng-if="data.type == '故事'">//带有故事数据的不同模板

<div ng-if="data.type == '文章'">//带有文章数据的不同模板

I am working on Angular app. I tried to use ng-if and switch inside ng-repeat but didn't succeed. I have data like:

   **[{"_id":"52fb84fac6b93c152d8b4569",
       "post_id":"52fb84fac6b93c152d8b4567",
       "user_id":"52df9ab5c6b93c8e2a8b4567",
       "type":"hoot",},  
      {"_id":"52fb798cc6b93c74298b4568",
       "post_id":"52fb798cc6b93c74298b4567",
       "user_id":"52df9ab5c6b93c8e2a8b4567",
       "type":"story",},        
      {"_id":"52fb7977c6b93c5c2c8b456b",
       "post_id":"52fb7977c6b93c5c2c8b456a",
       "user_id":"52df9ab5c6b93c8e2a8b4567",
       "type":"article",},**

$scope.comments = data mentioned above
and my Html like :

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

How can I achieve this thing in Angular?

解决方案

Try to surround strings (hoot, story, article) with quotes ':

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

这篇关于使用 ng-if 作为 ng-repeat 中的开关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆