2016-07-28 33 views
0

我有一些奇怪的行爲正在用ng-show進行。我有一個posttitlelink屬性。鏈接可能存在也可能不存在。如果是這樣,我想將標題顯示爲鏈接,否則只顯示標題。ng顯示奇怪的行爲發生了什麼事?

(行:<a href="{{post.link}}"> {{post.link}}</a>是測試代碼,而只是在那裏說明{{post.link}}呢,其實,絕對包含的值)

<a ng-show="{{post.link}}" href="{{post.link}}"> 
      {{post.title}} 
    </a> 

    <a href="{{post.link}}"> {{post.link}}</a> 

給我:

enter image description here

所以,你可以看起來像{{post.link}}存在,可以訪問和填充......但仍然以某種方式評估爲假,只要ng-show是關心的,並且不顯示。我能看到的只是測試代碼的結果。

鑑於,如果我評估true,甚至{{post.title}},那麼ng顯示工作。

我很困惑 - 我錯過了什麼?

<a ng-show="true" href="{{post.link}}"> 
     {{post.title}} 
</a> 
<a href="{{post.link}}"> {{post.link}}</a> 

<a ng-show="{{post.title}}" href="{{post.link}}"> 
     {{post.title}} 
</a> 
<a href="{{post.link}}"> {{post.link}}</a> 

給我:

enter image description here

+0

以及下面,使用ng-href請參閱https://docs.angularjs.org/api/ng/directive/ngHref –

回答

3

伍秀並不需要{{ ... }}因爲它直接使用的變量。將其更改爲

ng-show="post.link" 

{{ ... }}當你想輸出是在DOM可見時才使用。 ng-show是有角度的,所以你不需要它。

+0

D'oh !!!謝謝你 – kpollock