2016-09-06 42 views
-1

我有一個角度表達式{{ userInfo.activities.ids }},它是一個數組,我通過php發送給角。
現在我需要這個數組中檢查一個值,
我使用這個: -ng - 如果行爲不如預期

[[userInfo.activities.ids.indexOf(1) != -1]]和它返回true或false。
我想根據它的價值來應用ng-if。 因此,我寫道:

<input ng-if="{{userInfo.activities.ids.indexOf(1) != -1}} == true" checked type="checkbox"> 
<input ng-if="{{userInfo.activities.ids.indexOf(1) == -1}} == true" type="checkbox"> 

ng-if沒有驗證條件,因此任何輸入的被顯示出來。

請提出我可以用什麼方法代替這個?
對於從角度表達式分離我的模板我已經使用[[]]插值。
我使用的角度與laravel 5.2 PHP一起。

回答

1

ng-if條件刪除您的括號,

如果您正在使用的角度等車型ng-if, ng-model, ng-class等變量的作用域,就沒有必要使用插值(大括號)。

也爲您的表達式的計算結果爲真或假,你就沒必要再次檢查== true條件

<input ng-if="((userInfo.activities.ids.indexOf(1)) != -1)" checked type="checkbox"> 
<input ng-if="((userInfo.activities.ids.indexOf(1)) == -1)" type="checkbox"> 

Here is a reference

+0

讓我執行並檢查解決方案。謝謝@Sravan。 – Simer

+0

它的工作。謝謝!!當我應用ng-if =「{{userInfo.activities.ids.indexOf(1)!= -1}} =='true'」時,它也起作用。即當我在報價中輸入真/假時。謝謝你的幫助。 – Simer

+0

這是我作爲前端開發人員在2年職業生涯中遇到的最糟糕的情況。使用我的答案的檢查 – CodeNashor

3

請檢查您的數組是這樣的:

<input ng-if="userInfo.activities.ids[0] === -1" type="checkbox"> // if the value is a number 
<input ng-if="userInfo.activities.ids[0] === '-1'" type="checkbox"> // it its a string 
<input ng-if="userInfo.activities.ids.length === 0'" type="checkbox"> // if its empty 

身份(===)運算符行爲相同,除了沒有類型轉換,等於(==)運算符做的,而類型必須相同被認爲是平等的。始終使用===或!==

+0

我要檢查的1,2,3指數,因此在我的陣列和在此基礎上,我必須申請檢查。正如所解釋的那樣。感謝@codeNashor的幫助。 – Simer