如何使用C#将lib的@Component({
selector: 'my-app',
providers: [FormBuilder],
template: `
<div>
Checkbox values: {{alertForm.value | json}}
<mark> Is Form Valid: {{alertForm.valid}} </mark> <-- WHY IS FORM VALID ALWAYS FALSE?
Does Fields have error: {{alertForm.get('fields').hasError('checkboxSelected')}}
</div>
<form [formGroup]="alertForm">
<fieldset class="form-group" formGroupName="fields">
<legend class="col-md-4"> Fields</legend>
<div class="col-md-8 checkbox column-2">
<label *ngFor="let type of types.controls; let i=index">
<input name="fieldsAdd" type="checkbox" [formControl]="type" />
{{fields[i].name}}
</label>
</div>
</fieldset>
</form>
`
})
export class App implements OnInit {
alertForm: FormGroup;
fields = [{name:"field1", checked: false}, {name:"field2", checked: false}];
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.alertForm = this.fb.group({
fields: this.fb.array([], this.checkboxValidator)
});
const controlArray = <FormArray>this.alertForm.controls['fields'];
this.fields.map(field => controlArray.push(this.fb.control(field.checked)));
}
checkboxValidator(control: AbstractControl) {
return { 'checkboxSelected': !(control.value.indexOf(true) >= 0) };
}
get types(): FormArray {
return this.alertForm.get('fields') as FormArray;
};
}
属性转换为foreach?我需要对每个字符串做一些事情,我需要一种foreach方式吗?任何人都可以提供帮助。
id
这是我尝试过的,虽然它返回null。
<map>
<lib id="blue" revision="58399">
<part id="2732" type="ch"/>
<part id="2732" type="ls"/>
<part id="2732" type="rs"/>
<part id="2733" type="ch"/>
</lib>
<lib id="black" revision="26609">
<part id="2109" type="cc"/>
<part id="2109" type="lc"/>
<part id="2109" type="rc"/>
</lib>
</map>
答案 0 :(得分:1)
有多种方法可以获得id
元素的lib
属性。
这是公共代码(xml变量):
var xml = @"<map>
<lib id=""blue"" revision=""58399"">
<part id=""2732"" type=""ch""/>
<part id=""2732"" type=""ls""/>
<part id=""2732"" type=""rs""/>
<part id=""2733"" type=""ch""/>
</lib>
<lib id=""black"" revision=""26609"">
<part id=""2109"" type=""cc""/>
<part id=""2109"" type=""lc""/>
<part id=""2109"" type=""rc""/>
</lib>
</map>";
这是一个:
var xdoc = XDocument.Parse(xml);
foreach (var lib in xdoc.Descendants("lib"))
Console.WriteLine(lib.Attribute("id").Value);
2:
var xdoc = XDocument.Parse(xml);
foreach (var item in xdoc.Root.Elements("lib"))
Console.WriteLine(item.Attribute("id").Value);
三(使用XPath):
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
foreach (XmlAttribute attr in xmlDoc.SelectNodes("/map/lib/@id"))
Console.WriteLine(attr.Value);