简体   繁体   English

ng-if无法正常工作

[英]ng-if doesn't works as expected

I have a buttons array in my model, and an 'ng-if' in the template for each button: 我的模型中有一个按钮数组,每个按钮的模板中都有一个“ ng-if”:

self.aButtons.push({
                label: __('Siguiente'),
                class: 'btn-moduloAvance',
                show: (self.oContrato.definirPago || self.oContrato.horario_definido)
                }
            });

That's the Html: 这就是HTML:

  <button ng-if="boton.show"
          ng-repeat="boton in personaAndContrato.aButtons"
          type="button"
          ng-class="boton.class"
  />

I know that i'm assigning a boolean to the 'show' property, so it's impossible it changes because it's a simple type. 我知道我正在为'show'属性分配一个布尔值,因此它不可能更改,因为它是一种简单的类型。

By the way I'm trying to assign a function to the 'show' property but i'm getting the following error: 顺便说一句,我试图将一个函数分配给'show'属性,但出现以下错误:

    self.aButtons.push({
                label: __('Siguiente'),
                class: 'btn-moduloAvance',
                show: function () {
                    return (self.oContrato.definirPago || self.oContrato.horario_definido);
                }
            });

Html (edited): HTML(已编辑):

<button ng-if="boton.show()"
      ng-repeat="boton in personaAndContrato.aButtons"
      type="button"
      ng-class="boton.class"
/>

TypeError: v2.show is not a function at fn (eval at compile (angular.js?version=alpha.1:14539), :4:240) TypeError:v2.show不是fn的函数(在编译时评估(angular.js?version = alpha.1:14539),: 4:240)

Some ideas? 有什么想法吗? Does i'm missing or missunderstanding something? 我是否缺少或误解了什么?

Thanks in advance! 提前致谢!

You can't have ng-repeat and ng-if in the same tag if ng-if refer to the inner variable of ng-repeat . 如果ng-if引用ng-repeat的内部变量, ng-if在同一标记中包含ng-repeatng-if

Just split it : 只是拆分:

<div ng-repeat="boton in personaAndContrato.aButtons">
    <button ng-if="boton.show()"
      type="button"
      ng-class="boton.class"
   />
</div>

更改为ng-if="boton.show()"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM