如何将用于值的对象传递给* NgIf中使用的函数?

时间:2019-01-27 13:37:39

标签: angular angular-ng-if

我想对Angular 5应用程序的所有核心逻辑进行单元测试。我意识到我的* NgIf是核心逻辑。这样,我喜欢将它们提取到ShowHide助手类。这样一来,我不想最终使用(变量,变量,变量)函数来执行诸如检查原始,有效等逻辑操作。

我尝试了下面的功能

public ValidOrPristine(checkControl:AbstractControlDirective):boolean{
    console.warn(checkControl);
    if(checkControl.pristine || checkControl.valid){
        return false;
    } else {
        return true;
    }
}

然后在我的html中替换

<input type="text" #inputItem=[(ngModel)]>
<div [hidden]="inputItem.valid || inputItem.pristine">

具有:

<input type="text" #inputItem=[(ngModel)]>
<div [hidden]="showHide.ValidOrPristine(inputItem)">

我的元素总是显示并查看控制台输出,该函数接收到未定义的信息。

好吧,接下来我尝试了

<input type="text" #inputItem=[(ngModel)]>
<div [hidden]="showHide.ValidOrPristine(this)">

现在,控制台正在其中显示整个对象。不太有用。

我想验证一下这确实是可能的,因此我写:

public Value1OrValue2(value1:boolean,value2:boolean):boolean{
    if(value1 || value2){
        return false;
    } else {
        return true;
    }
}

将其集成到:

<input type="text" #inputItem=[(ngModel)]>
<div [hidden]="showHide.Value1OrValue2(inputItem.valid,inputItem.pristine)">

就像冠军一样,隐藏元素。

我希望能够通过控件来执行访问控件属性的幕后逻辑。

I even went as far as to look at the NgIf in the angular Git repository.

然后我承认我的技能水平目前还无法追查。

老实说,我认为通过NgIf的逻辑并不难。确定正确的类型,我认为是绊脚石。所有这些都是为了使用像Jasmine这样的东西来遍历这些项目上所有复杂的逻辑。

0 个答案:

没有答案