0

我想将ngIf指令应用于 DOM 元素,这个ngIf监视一个函数,那个函数检查其他变量的值。如下:

    export class Controller{

    private x: ObjectClass;

        public isDone():boolean{
         return this.x.isY() && this.otherFunction();
        }

        private otherFunction():boolean{
        let result:boolean = true;
        this.array.forEach((value)=>{
        if(!value){
        result = false;}
        });
        return result
        }
    }

//define the state 

  .state('app.first-state', {
        url: '/first-state,
        views: {
          'renderView': {
            template: require('./states/first-state.html'),
            controller: 'Controller as Ctrl',
          }
        }
      })

//in the first-state.html

    <div ng-if="Ctrl.isDone()"></div>

x是一个携带方法和变量的对象。

x.isY()并将其this.otherFunction()返回值更改为,true但 ngIf 不会重新创建 DOM 元素。似乎摘要循环既不关注x对象也不关注array对象。这是有道理的,因为我没有将它们直接用于 DOM 元素。但是,该方法isDone()仅在我进入该状态或退出该状态时运行。

我做了以下解决方法,但我担心会出现性能问题:

    private isDoneVar:boolean = false;

    //then make my "own digest cycle"
     $interval(() => {
                this.isDoneVar = this.isDone();
            }, 700);
//and in the div
        <div ng-if="Ctrl.isDoneVar"></div>

我有两个问题:

  1. 为什么isDone进入和离开状态时函数会运行?
  2. 有没有更好的方法来告诉 angular 在每个摘要循环运行时运行这个函数?或者观察其内部变量的任何变化(不添加$watch到每个变量)
4

0 回答 0