小编Pau*_*ton的帖子

角度2模型驱动的嵌套表单组件

是)我有的:

我正在构建一个离子2应用程序,并构建了一个包含的基本角度2组件

  • 输入字段

  • 用于显示输入标题的标签

  • 用于显示任何验证错误的标签

我将此称为我的输入组件

我有一个页面组件,上面有一个表单,目前有文本输入.1个常规输入(密码)和1个输入包装在我的输入组件(用户名)中.

这是我的页面组件的相关部分

ngOnInit() {
  this.loginForm = this.formBuilder.group({
    username: ['', Validators.required],
    password: ['', Validators.required]
  });
}
Run Code Online (Sandbox Code Playgroud)

这是页面组件模板

<form [formGroup]="loginForm" (ngSubmit)="onSubmit()">

  <!-- My input component -->
  <aw-input-text id="username" name="Username" [formInput]="loginForm.controls.username"></aw-input-text>

  <!-- A standard input control -->
  <ion-item [class.error]="loginForm.controls.password.errors">
    <ion-label floating>Password</ion-label>
    <ion-input type="text" value="" name="password" formControlName="password"></ion-input>
    <p *ngIf="loginForm.controls.password.errors">This field is required!</p>
  </ion-item>

  <button type="submit" class="custom-button" [disabled]="!loginForm.valid" block>Login</button>

</form>
Run Code Online (Sandbox Code Playgroud)

这是我输入组件的模板

<!-- Component template -->
<form [formGroup]="formGroup">
    <ion-item>
        <ion-label floating>{{inputName}}</ion-label>
        <ion-input type="text" formControlName="inputValue"></ion-input>
        <p *ngIf="!formGroup.controls.inputValue.valid">This field is required!</p>
    </ion-item> …
Run Code Online (Sandbox Code Playgroud)

javascript forms angular2-forms angular2-components angular

7
推荐指数
1
解决办法
3072
查看次数

在Angular 4单元测试中未调用ngOnChanges detectChanges()

问题

我有一个按钮组件,它接受承诺并禁用按钮,直到承诺解决为止,并且我想为此功能编写单元测试。

我的密码

我的按钮组件有一个承诺的输入

  /**
   * A single promise that triggers the busy state until it resolves
   */
  @Input()
  public promise: Promise<any>;
Run Code Online (Sandbox Code Playgroud)

在ngOnChanges内部,我听了承诺

/**
 * Enable button busy state until promise resolves
 */
if (changes && changes.promise && changes.promise.currentValue) {
  this.busyUntilPromiseResolves(changes.promise.currentValue);
}
Run Code Online (Sandbox Code Playgroud)

然后,我存储了一个有效的promise数组,以便可以传递多个promise

  /**
   * Add a promise that will keep the button in a busy state until it resolves
   * @param activityProimise
   */
  private busyUntilPromiseResolves(activityProimise) {
    this.activityPromises.push(activityProimise);
    activityProimise.then(() => {
      this.activityPromises.splice(this.activityPromises.indexOf(activityProimise), 1);
    });
  }
Run Code Online (Sandbox Code Playgroud)

然后最后在我的模板中,如果数组中有任何promise,我将禁用按钮。

[disabled]="activityPromises.length > 0"
Run Code Online (Sandbox Code Playgroud)

帮我带完 …

unit-testing promise angular-components angular angular-unit-test

5
推荐指数
1
解决办法
1828
查看次数

nodejs使用cheerio解析xml返回空CDATA

我在nodejs中使用cheerio来解析一些rss feed.我抓住所有将它们放入阵列的物品.我使用3个测试源,它们都为每个"item"元素都有一个"description"子元素.在其中一个Feed中,整个"描述"被包装为CDATA,我无法获得它的价值.这是一个缩写的代码片段

//Open the xml document with cheerio
$ = cheerio.load(arrXmlDocs[i],{ ignoreWhitespace : true, xmlMode : true});

//Loop through every item
$('item').each(function(i, xmlItem){

    //array to hold each item being converted into an array
    var tempArray = [];

    //Loop through each child of <item>
    $(xmlItem).children().each(function(i, xmlItem){
        //Get the name 
        tempArray[$(this)[0].name] = $(this).text();
    }

}
Run Code Online (Sandbox Code Playgroud)

正如所料,没有CDATA的两个RSS提供给我一个像这样的数组

[
    [
        name: 'name of episode',
        description:'description of episode',
        pubdate: 'published date'
    ],
    [
        name: 'name of episode',
        description:'description of episode',
        pubdate: 'published date'
    ]
] …
Run Code Online (Sandbox Code Playgroud)

javascript xml cdata node.js cheerio

3
推荐指数
1
解决办法
3062
查看次数