This seems like it should be straight-forward, but I can't see what I'm doing wrong. I have a form on which some components are optionally enabled based on a yes/no radio button. I'm attempting to set the disabled attribute based on the boolean behind that radio button. If I set [disabled] to the radio button every thing works (but is reversed from the desired behavior). If I set [disabled] to the negation of the boolean, it suddenly doesn't work. I wrote up a Plunker to illustrate: NegationConditionExample. Simply remove the '!' from the oResponded variable and it will work but be reversed from the desired behavior.
Here's the code:
//our root app component
import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
@Component({
selector: 'my-app',
template: `
<form (ngSubmit)="onSubmit()" #resolveForm="ngForm">
<div class="form-group">
<label> Did anyone respond? </label>
<div class="radio">
<label>
<input type="radio"
name="responded"
[(ngModel)]="oResponded"
#responded="ngModel"
value=true> Yes someone responded
</label>
</div>
<div class="radio">
<label>
<input type="radio"
name="responded"
[(ngModel)]="oResponded"
#responded="ngModel"
value=false> No
</label>
</div>
</div>
<div class="form-group">
<label for="responseType">What type of response?</label>
<select class="form-control" id="responseType"
required
[(ngModel)]="oResponseType" name="responseType"
#responseType="ngModel"
[disabled]="!oResponded">
<option *ngFor="let i of oResponseTypes" [value]="i">{{i}</option>
</select>
<div [hidden]="responseType.valid || responseType.pristine" class="alert alert-danger">
Response Type is Required
</div>
</div>
oResponded: {{oResponded}}
</form>
`,
})
export class App {
oResponded: boolean;
oResponseType: string;
oResponseTypes: string[] = ["Agreed", "Disagreed", "Other"];
constructor() {
this.oResponded = false;
}
}
@NgModule({
imports: [ BrowserModule, FormsModule, CommonModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}`