Angular - 表单验证 - 无法读取未定义的属性“名称”

时间:2016-05-30 04:52:39

标签: javascript angularjs

所以我有一个带有输入和其他东西的表单,我正在尝试进行一些角度验证,以确保输入的信息实际存在(非空白)。为此,我正在使用Cannot read property 'name' of undefined语句。

我得到的错误信息是:

<input>

如果它留空,似乎无法读取if标记名称。当我填写,但不是其他人(这是一个和。我只是尝试使用reviewModal.view.html (shortened form version)语句来查看它们是否已被填写时,该函数有效。这是下面的html和角度代码:

<div class="modal-content"> <div role="alert" ng-show="vm.formError" class="alert alert-danger">{{ vm.formError }}</div> <form id="addReview" name="addReview" role="form" ng-submit="vm.onSubmit()" class="form-horizontal"> <label for"name" class="col-xs-2 col-sm-2 control-label">Name</label> <div class="col-xs-10 col-sm-10"> <input id="name" name="name" ng-model="vm.formData.name" class="form-control"> </div> <button type="submit" class="btn btn-primary">Submit review</button> </form> </div>

reviewModal.controller.js

(function() { angular .module('loc8rApp') .controller('reviewModalCtrl', reviewModalCtrl); reviewModalCtrl.$inject = ['$uibModalInstance', 'locationData']; function reviewModalCtrl($uibModalInstance, locationData) { var vm = this; vm.locationData = locationData; vm.onSubmit = function() { vm.formError = ""; if(!vm.formData.name || !vm.formData.rating || !vm.formData.reviewText) { vm.formError = "All fields required, please try again"; return false; } else { console.log(vm.formData); return false; } }; vm.modal = { cancel : function() { $uibModalInstance.dismiss('cancel'); } }; } })();

locationDetail.controller.js

(function() { angular .module('loc8rApp') .controller('locationDetailCtrl', locationDetailCtrl); locationDetailCtrl.$inject = ['$routeParams', '$uibModal', 'loc8rData']; function locationDetailCtrl($routeParams, $uibModal, loc8rData) { var vm = this; vm.locationid = $routeParams.locationid; loc8rData.locationById(vm.locationid) .success(function(data) { vm.data = { location: data }; vm.pageHeader = { title: vm.data.location.name }; }) .error(function(e) { console.log(e); }); vm.popupReviewForm = function() { var modalInstance = $uibModal.open({ templateUrl: '/reviewModal/reviewModal.view.html', controller: 'reviewModalCtrl as vm', resolve : { locationData : function() { return { locationid : vm.locationid, locationName : vm.data.location.name }; } } }); }; } })();

    <!-- BLOG CONTENT
============================================= -->
<div class="container">
    <div class="row" id="primary">
       <main id="content" class="col-sm-8 role="main>

2 个答案:

答案 0 :(得分:4)

必须先定义

vm.formData才能在html中分配/读取name属性。将reviewModalCtrl中的代码更新为init vm.formData:

vm.formData = {};

答案 1 :(得分:0)

使用角度验证设置验证控制器中的内容,如下所示。

<div class="modal-content">
    <div role="alert" ng-show="addReview.$submitted && addReview.$invalid" class="alert alert-danger">All fields required, please try again</div>
    <form id="addReview" name="addReview" role="form" ng-submit="vm.onSubmit()" class="form-horizontal">
        <label for"name" class="col-xs-2 col-sm-2 control-label">Name</label>
            <div class="col-xs-10 col-sm-10">
                <input id="name" name="name" ng-model="vm.formData.name" class="form-control" required>
            </div>
        <button type="submit" class="btn btn-primary">Submit review</button>
    </form>
</div>

我在文本框中使用了 required ,并在上面显示错误消息 的 ng-show="addReview.$invalid"

您在控制器上不需要任何代码。