Angular JS - ng-required属性不起作用

时间:2016-04-09 06:47:00

标签: angularjs

我编写了以下代码,以便在用户未输入全名时在文本框下方显示范围。但是下面的代码不起作用。任何人都可以帮助理解这个吗?感谢

<div class="form-group">
         <label for="fullName" class="col-sm-3 control-label"> Name </label>
         <div class="col-sm-9">
              <input type="text" id="fullName" name="fullName" 
                           class="form-control"
                 ng-model="editableEmployee.fullName" ng-required="true" />
              <span ng-show="editableEmployee.fullName.$error.required">
                        Full name is required </span>
          </div>
    </div>

2 个答案:

答案 0 :(得分:3)

添加像这样的表单元素

<form name="myForm">
   <div class="form-group">
      <label for="fullName" class="col-sm-3 control-label"> Name </label>
      <div class="col-sm-9">
          <input type="text" id="fullName" name="fullName" class="form-control" ng-model="editableEmployee.fullName" ng-required="true" />
          <span ng-show="myForm.fullName.$error.required">Full name is required </span>
      </div>
    </div>
</form>

答案 1 :(得分:2)

错误在于我使用ng-model值进行ng-show。 ng-show变量应该引用Formname.FieldName其中ng-model应该引用ScopeObjectName.Variablename

Picture showing that the ng-model and ng-show are using the same name