2014-11-06 62 views
0

我寫angularjs應用,並有這個代碼塊,但只是第一個HTML綁定HTML的工作對我來說剛剛頁面工作NG綁定,HTML的一個

<div ng-bind-html='newsTitle'> 
    <div ng-bind-html='newsDetail'></div> 
</div> 

當我更改優先級這樣的:

<div ng-bind-html='newsDetail'> 
    <div ng-bind-html='newsTitle'></div> 
</div> 

它顯示newsDetail值。 我可以聲明每頁多少個ng-bind-html?爲什麼第二個值不顯示?

+0

是你的div標籤不關閉?無論如何,我不確定你可以把一個ng-bind-html放在另一個ng-bind-html中。 – enguerranws 2014-11-06 10:54:15

+0

他們有密切的標籤 – 2014-11-06 18:49:15

+0

是的,所以看看我的答案,它會解決您的問題。 – enguerranws 2014-11-07 10:34:59

回答

2

我想我理解您的問題。

<div ng-bind-html='newsTitle'> <!-- This will replace the content of the div with $scope.newsTitle --> 
    <div ng-bind-html='newsDetail'> <!-- So this won't appear, because it has been removed by ng-bind-html='newsTitle' --> 
    </div> 
</div> 

期待中的代碼我的意見。所以如果你把newsDetails放在第一位,綁定的HTML($ scope.newsDetail)也會替代當前的內容。

總之,ng-bind-html用您提供的綁定HTML替換元素的當前內容。所以你不應該把HTML放在那些元素中。

你只需做這樣的事情:

<div class="news"> 
<div ng-bind-html='newsTitle'></div> 
<div ng-bind-html='newsDetail'></div> 
</div> 

約ngBindHtml指令的一些文檔:https://docs.angularjs.org/api/ng/directive/ngBindHtml

1

如果它是你的html的真實副本。那麼我認爲這是結構問題。請關閉您的區塊:

<div> </div> 
1

你可以嘗試寫這樣

<div><span ng-bind-html='newsTitle'></span></div> 
<div><span ng-bind-html='newsDetail'></span></div>