AngularJS检查的形式是有效控制 [英] AngularJS check if form is valid in controller

查看:126
本文介绍了AngularJS检查的形式是有效控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查,如果一种形式是在控制有效。

查看:

 <形式的novalidate =
      NAME =createBusinessForm
      NG-提交=setBusinessInformation()
      类=CSS-形式>
 <! - 场 - >
< /表及GT;

在我的控制器:

  .controller(
    BusinessCtrl',
    功能($范围,$ HTTP,$位置,商务,的BusinessService,
              UserService,照片)
    {        如果($ scope.createBusinessForm $有效){
            $ scope.informationStatus =真;
        }        ...

我得到这个错误:

 类型错误:无法读取属性未定义'$有效


解决方案

我已经更新控制器:

  .controller('BusinessCtrl',
    功能($范围,$ HTTP,$位置,商务,的BusinessService,UserService,照片){
        $范围。$腕表('createBusinessForm。$有效',函数(的newval){
            //$scope.valid =的newval;
            $ scope.informationStatus =真;
        });
        ...

I need to check if a form is valid in a controller.

View:

<form novalidate=""
      name="createBusinessForm"
      ng-submit="setBusinessInformation()"
      class="css-form">
 <!-- fields -->
</form>

In my controller:

.controller(
    'BusinessCtrl',
    function ($scope, $http, $location, Business, BusinessService, 
              UserService, Photo)
    {

        if ($scope.createBusinessForm.$valid) {
            $scope.informationStatus = true;
        }

        ...

I'm getting this error:

TypeError: Cannot read property '$valid' of undefined

解决方案

I have updated the controller to:

.controller('BusinessCtrl',
    function ($scope, $http, $location, Business, BusinessService, UserService, Photo) {
        $scope.$watch('createBusinessForm.$valid', function(newVal) {
            //$scope.valid = newVal;
            $scope.informationStatus = true;
        });
        ...

这篇关于AngularJS检查的形式是有效控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆