ng-如果没有按预期表现

ng-if not behaving as expected

我有一个 angular 表达式 {{ userInfo.activities.ids }},它是一个数组,我通过 php.
发送到 angular 现在我需要检查这个数组中的一个值,
我正在使用这个:-

[[userInfo.activities.ids.indexOf(1) != -1]] 和它 returns 对或错。
我想根据它的价值应用 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 未验证条件,因此显示输入的 none。

请建议我可以用什么方法代替这个?
为了将我的模板与 angular 表达式分开,我使用了 [[]] 插值。
我正在使用 angular 和 laravel 5.2 php.

请像这样检查你的数组:

<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

标识 (===) 运算符的行为与相等 (==) 运算符相同,只是不进行类型转换,并且类型必须相同才能被视为相等。始终使用 === 或 !==

ng-if条件下取下牙套,

如果您在 angular 模型中使用范围变量,例如 ng-if, ng-model, ng-class 等,则无需使用插值(大括号)。

此外,由于您的表达式求值为 true 或 false,因此您无需再次检查 == 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