如果选择了另一个元素,则应用ng-required

时间:2017-09-29 11:51:51

标签: javascript angularjs ng-required

我正在创建表单的可选部分。一旦选择了两个元素中的一个(或输入了文本),则需要另一个元素,但如果没有这两个字段的输入则不需要。我可以用ng-required完成这个吗?如果没有另外的方法?感谢您的帮助!

Example Form

2 个答案:

答案 0 :(得分:0)

您可以使用其他模型作为参数执行<input ng-model="myFirstInput" /> <input ng-model="mySecondInput" ng-required="myFirstInput" />

myFirstInput

如果填写了第一个输入,func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MyCustomCell for i in 0...nameOfRoutesStart.count { if (indexPath.row == i) { cell.originLabel.text = nameOfRoutesStart[i] cell.destinationLabel.text = nameOfRoutesEnd[i] let id = driver[i] self.userTasks.user(driverId: id, completionHandler: { (status, success) -> Void in if success { cell.driverLabel.text = self.userTasks.username } }) cell.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping } } return cell } 将评估为真,那么第二个输入将是必需的。

请参阅enter image description here

答案 1 :(得分:0)

您可以使用ng-required指令

<form name="myForm">

 Click here to make the input field required:

 <input type="checkbox" ng-model="myVar"><br><br>

 <input name="myInput" ng-model="myInput" ng-required="myVar">

 <h1 ng-if="!myForm.myInput.$valid">The input field cannot be empty</h1>

 </form>