ng-click后如何切换字段必需属性?

时间:2014-01-31 18:27:25

标签: angularjs angularjs-directive angularjs-ng-change

我在转发器中有一个下拉列表,应该切换我的自定义“必需”属性。我尝试了ng-show但是display =“none”属性就是所有添加的。我的选择是: -

1 - 添加/删除输入字段并设置bird.Stuff,而不仅仅是隐藏它,因为它仍然是必需的。

2 - 在输入字段中添加/删除'required'属性。

JS

$scope.validateParticipants = function (type) {
    if (type == "Single") {
        this.donation.Participants = "1";
    }
    else
        this.donation.Participants = "";
}

HTML

 <div ng-repeat="bird in animalTest.birds">
    @(Html.DropDownList("Type", (IEnumerable<SelectListItem>)ViewBag.BirdList, new { ng_model = "bird.Type", ng_change = "validateRequired(bird.Type)", required = "Type is required" }))
    ...
    <input data-ng-show="bird.Type == 'Endangered'" id="stuff" type="number" data-ng-model="bird.Stuff" required = "Number of Volunteers required"/>  
</div>

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:9)

使用ng-required。如果表达式为真,则采用角度表达式并添加必需属性。

<input type="number" data-ng-model="bird.Stuff" ng-required="donation.Participants > 0"/>