2017-12-19 4 views
1

로컬 저장소에 데이터를 저장해야하지만 양식을 확인해야합니다. SavelocalStorage 함수를 호출 한 후 실수가 있습니다 TypeError: Cannot read property '$valid' of undefined. 뭐가 문제 야? 어떤 도움을 주신

ps $scope.myForm.$valid의 경우 myForm.$valid으로 바뀌면 유효하지만 부적절한 방법입니다. 모든 간격을 채운 후에도 팝업 경고 경고가 계속 표시됩니다. 당신이 부모와 함께 controllerAs 구문을 사용하는 경우

var app = angular.module("myApp",['listOfBooks']); 
 
      app.controller("myCtrl", function($scope){ 
 
       $scope.authors = []; 
 

 
       $scope.addAuthor = function(){ 
 
         var author = {}; 
 
         author.surname = ""; 
 
         author.name = ""; 
 
         $scope.authors.push(author); 
 
       }; 
 
       $scope.SavelocalStorage = function(){ 
 
        if($scope.myForm.$valid){ 
 
         localStorage.setItem('Authors', JSON.stringify($scope.authors)); 
 
        } 
 
        else{ 
 
         alert("fill in all the gaps pls!"); 
 
        } 
 
       }; 
 
      }); 
 
      
 
      var app = angular.module("listOfBooks", []); 
 
     app.controller("booksCtrl", function($scope) { 
 
      $scope.showBooks = false; 
 
     
 
      $scope.currentAuthor = {}; 
 
      $scope.showBookList = function(author) { 
 
      $scope.showBooks = !$scope.showBooks; 
 
      $scope.currentAuthor = author; 
 
      } 
 
     
 
      $scope.addBook = function() { 
 
      
 
      $scope.currentAuthor.books = $scope.currentAuthor.books || []; 
 
      var book = {}; 
 
      book.title = ""; 
 
      $scope.currentAuthor.books.push(book); 
 
      }; 
 
     });
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script> 
 
    <link href="https://netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> 
 
    
 
</head> 
 

 
<body ng-app="myApp" ng-controller="myCtrl"> 
 
    <div class="container"> 
 
    <h3>AUTHORS' LIST</h3> 
 
    <div class="btn-group"> 
 
     <button ng-click="addAuthor()" type="button" class="btn btn-default">Add</button> 
 
     <button ng-click="SavelocalStorage()" type="button" class="btn btn-default">Save</button> 
 
    </div> 
 
    <form ng-controller="booksCtrl" name="myForm"> 
 
     <table class="table table-bordered"> 
 
     <tr> 
 
      <th>Surname</th> 
 
      <th>Name</th> 
 
      <th>Books</th> 
 
     </tr> 
 
     <tr ng-repeat="author in authors"> 
 
      <td><input ng-model="author.surname" required type="text" class="form-control"></td> 
 
      <td><input ng-model="author.name" required type="text" class="form-control"></td> 
 
      <td> 
 
      <button ng-click="showBookList(author)" type="button" class="btn btn-default">List</button> 
 
      </td> 
 
     </tr> 
 
     </table> 
 

 
     <div ng-show="showBooks" class="col-sm-8" style="background-color:lightblue; position: absolute; left:5px; top:5px;z-index:2;"> 
 
     <div class="btn-group"> 
 
      <button ng-click="addBook()" type="button" class="btn btn-default">Add</button> 
 
     </div> 
 
     <table class="table table-bordered"> 
 
      <tr> 
 
      <th>Title</th> 
 
      </tr> 
 
      <tr ng-repeat="book in currentAuthor.books"> 
 
      <td><input ng-model="book.title" type="text" class="form-control"></td> 
 
      </tr> 
 
     </table> 
 
     <button class="btn btn-sm btn-warning" ng-click="showBooks = false">Close</button> 
 
     </div> 
 
    </form> 
 
    </div> 
 
     </body> 
 
</html>

+0

(_had jsfiddle_에서 그것을 확인) $ scope.myForm''와 구문은 올바른 것입니다. 그러나 문제는 중첩 된 컨트롤러가 있다는 것입니다. '$ scope.myForm'은 폼이 자식 컨트롤러'booksCtrl'에 있기 때문에 정의되지 않았습니다. 외부 컨트롤러 인'myCtrl'에서 액세스하려고합니다. –

+0

유효성 검사 섹션을 내부 컨트롤러로 이동해야합니까? –

답변

2

당신은, $scope 상속 조금 놀 필요가 절약 LS 데이터와 모든 권리는 데이터 유효성 검사를 제거하는 경우

컨트롤러 내에서 양식 이름을 booksCtrl으로 설정하면 으로, 자바 스크립트의 프로토 타입 상속 때문에 form이 parent $ scope에 등록되고 부모 $scope의 양식에서 유효성 검사를 호출 할 수 있습니다 .

var app = angular.module("myApp",['listOfBooks']); 
 
      app.controller("myCtrl", function($scope){ 
 
       $scope.authors = []; 
 
       var vm = this; 
 
       $scope.addAuthor = function(){ 
 
         var author = {}; 
 
         author.surname = ""; 
 
         author.name = ""; 
 
         $scope.authors.push(author); 
 
       }; 
 
       $scope.SavelocalStorage = function(){ 
 
        if(vm.myForm.$valid){ 
 
         localStorage.setItem('Authors', JSON.stringify($scope.authors)); 
 
        } 
 
        else{ 
 
         alert("fill in all the gaps pls!"); 
 
        } 
 
       }; 
 
      }); 
 
      
 
      var app = angular.module("listOfBooks", []); 
 
     app.controller("booksCtrl", function($scope) { 
 
      $scope.showBooks = false; 
 
     
 
      $scope.currentAuthor = {}; 
 
      $scope.showBookList = function(author) { 
 
      $scope.showBooks = !$scope.showBooks; 
 
      $scope.currentAuthor = author; 
 
      } 
 
     
 
      $scope.addBook = function() { 
 
      
 
      $scope.currentAuthor.books = $scope.currentAuthor.books || []; 
 
      var book = {}; 
 
      book.title = ""; 
 
      $scope.currentAuthor.books.push(book); 
 
      }; 
 
     });
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script> 
 
    <link href="https://netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> 
 
    
 
</head> 
 

 
<body ng-app="myApp" ng-controller="myCtrl as vm"> 
 
    <div class="container"> 
 
    <h3>AUTHORS' LIST</h3> 
 
    <div class="btn-group"> 
 
     <button ng-click="addAuthor()" type="button" class="btn btn-default">Add</button> 
 
     <button ng-click="SavelocalStorage()" type="button" class="btn btn-default">Save</button> 
 
    </div> 
 
    <form ng-controller="booksCtrl" name="vm.myForm"> 
 
     <table class="table table-bordered"> 
 
     <tr> 
 
      <th>Surname</th> 
 
      <th>Name</th> 
 
      <th>Books</th> 
 
     </tr> 
 
     <tr ng-repeat="author in authors"> 
 
      <td><input ng-model="author.surname" required type="text" class="form-control"></td> 
 
      <td><input ng-model="author.name" required type="text" class="form-control"></td> 
 
      <td> 
 
      <button ng-click="showBookList(author)" type="button" class="btn btn-default">List</button> 
 
      </td> 
 
     </tr> 
 
     </table> 
 

 
     <div ng-show="showBooks" class="col-sm-8" style="background-color:lightblue; position: absolute; left:5px; top:5px;z-index:2;"> 
 
     <div class="btn-group"> 
 
      <button ng-click="addBook()" type="button" class="btn btn-default">Add</button> 
 
     </div> 
 
     <table class="table table-bordered"> 
 
      <tr> 
 
      <th>Title</th> 
 
      </tr> 
 
      <tr ng-repeat="book in currentAuthor.books"> 
 
      <td><input ng-model="book.title" type="text" class="form-control"></td> 
 
      </tr> 
 
     </table> 
 
     <button class="btn btn-sm btn-warning" ng-click="showBooks = false">Close</button> 
 
     </div> 
 
    </form> 
 
    </div> 
 
     </body> 
 
</html>

관련 문제