2015-10-22 21 views
1

我有一個網站,其中有30個條目。到現在爲止,這就像300行HTML代碼和angularjs,現在它像10行。但其中一些文章很特殊,所以我需要更具體地展示什麼。下面是HTML代碼:ng-repeat在if語句中的角度

<div id="illustratorcontent" ng-repeat="illustrator in illustrators"> 
     <article id="illustratorcontent{{illustrator.ID}}"> 
      <header> 
       <h3>{{illustrator.name}}</h3> 
      </header> 
      <div class="illustratorcontentext"> 
       <p>This work is copyrighted and owned by {{illustrator.name}}...</p> 
       <p>Details of the copyright holder: </p> 
       <ul class="listplacing"> 
        <li>Name: {{illustrator.name}}</li> 
        <li>Website: <a href="{{illustrator.website}}">{{illustrator.name}}'s website</a></li> 
        <li>Website: <a href="{{illustrator.website2url}}">{{illustrator.name}}'s {{illustrator.website2text}}</a></li> 
       </ul> 
      </div> 
     </article> 
</div> 

和IllustratorController是:

function Illustrator ($scope){ 
$scope.illustrators = [ 
    { 
     "ID" : "Ilus1", 
     "name" : "Ilus1", 
     "website" : "http://Ilus1page.com/" 
    },{ 
     "ID" : "Ilus2", 
     "name" : "Ilus2", 
     "website" : "http://Ilus2page.com/" 
    }, ... 
    { 
     "ID" : "IlusX", 
     "name" : "IlusX", 
     "website" : "http://IlusXpage.com/" 
     "website2text" : "Twitter IlusX", 
     "website2url" : "http://twitterilusx.com/" 
    } 
] 
} 

所以我的問題是,我該怎麼讓現有的信息,並留下了出在沒有信息中給出。

所以在這條線:

<li>Website: <a href="{{illustrator.website2url}}">{{illustrator.name}}'s {{illustrator.website2text}}</a></li> 

我怎麼能說是給定對象的參數或不爲空時,此行只是表示。我已經找過一些方法。有一種叫做ng-ifng-ui但是這對我來說並沒有解決。還有一種方法可以通過類和隱藏div來實現它。但實際上我寧願不生成它們。而website2url/website2text只是一個例子,有更多的值比這個。

類似的問題,但我是一個比較特殊的,我相信:

+0

你能解釋一下NG-如果你沒有工作? – JMK

+0

ng-show或ng-if – sambomartin

+0

你可以在jsfiddle中添加代碼 – Mitul

回答

1

您好用於創建過濾器顯示不喜歡的鏈接

<li ng-if="showSecondWebLink(illustrator)">....</li> 

在你的控制器創建功能

$scope.showSecondWebLink = function(illustrator){ 
    if(illustrator.website2url && illustrator.website2text){ 
     return true; 
    } 
    return false; 
} 
+0

其實我喜歡把這些邏輯放入控制器的方式。我在你的代碼中解決了這個小問題。謝謝。 – kwoxer

+0

你能告訴我代碼有什麼問題,所以我會更新。 – Mitul

+0

那麼有人刪除我的更改...我剛剛刪除了**!=''**然後它的作品。 – kwoxer

1

只是嘗試:

<li ng-if="illustrator.website2url && illustrator.website2text">...</li> 
+0

我只是用這種方法試過了'''ng-if =「illustrator.website2url!=''」'''。這太容易了,謝謝我的朋友,它工作正常。 – kwoxer

+1

@kwoxer,'null!=''' – Grundy