ng-min基于条件Angularjs

时间:2017-03-20 16:33:39

标签: angularjs angularjs-directive

刚开始用angularjs开发, 谷歌搜索了一段时间,但没有成功,我想我正在做的不是正确的方式,它不起作用。 有人可以帮助我设置条件ng-min,所以年和月的字段不能同时为0,但其中一个可能是。谢谢大家

<md-input-container>
                    <label>Years</label>
                    <input ng-model="employer.years" type="number" ng-min="0" ng-max="50" ng-change="calculateMonth()" ng-required="employer.month == '0'" name="years">
                    <div ng-messages="employerForm.years.$error" role="alert" multiple>
                        <div ng-message="max" class="my-message">Max allowed value is 50
                        </div>
                        <div ng-message="min" class="my-message">Please enter a valid number from 0 to 50
                        </div>
                        <div ng-message="required" class="my-message">This field is required
                        </div>
                        <div  class="my-message"><p ng-bind="msg"></p>
                        </div>
                    </div>
                </md-input-container>
            </div>
  <div layout="row">
                <md-input-container>
                    <label>Months</label>
                    <input ng-model="employer.month" type="number"   ng-min="0" ng-max="11" ng-required="employer.years == '0'" name="month">
                    <div ng-messages="employerForm.month.$error" role="alert" multiple>
                        <div ng-message="max" class="my-message">Max allowed value is 11, you can always add a year
                        </div>
                        <div ng-message="min" class="my-message">Please enter a valid number from 0 to 11
                        </div>
                        <div ng-message="required" class="my-message">This field is required
                        </div>
                    </div>
                </md-input-container>

控制器代码

 $scope.calculateMonth = function () {
            if($scope.employer.years){
                    $scope.employer.month = 0;
                    if($scope.employer.month === 0 && $scope.employer.years === 0){
                        console.log('both are zero');
                        $scope.msg = 'Both values can not be 0'
                    }
                    $scope.msg = "Something Entered";
        }};

0 个答案:

没有答案