2017-01-31 14 views
0

htmlフォーム要素のコメントをangularjsの配列に追加しようとしています。 javasciptのプッシュ機能を使用すると、「オブジェクトのプロパティが存在しません」というエラーが表示されます。 誰かが私にこの問題の解決に手伝ってもらえますか?以下は、私のJavaScriptとHTMLコードを見つけることができます。javascriptの配列に新しい要素をプッシュするときにエラーが発生する

は、私はそれが各コントローラのスコープとは何かを持っていると信じて

.controller('DishDetailController', ['$scope', function($scope) { 
 

 
      var dish={ 
 
          name:'Uthapizza', 
 
          image: 'images/uthapizza.png', 
 
          category: 'mains', 
 
          label:'Hot', 
 
          price:'4.99', 
 
          description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.', 
 
          comments: [ 
 
           { 
 
            rating:5, 
 
            comment:"Imagine all the eatables, living in conFusion!", 
 
            author:"John Lemon", 
 
            date:"2012-10-16T17:57:28.556094Z" 
 
           }, 
 
           { 
 
            rating:4, 
 
            comment:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!", 
 
            author:"Paul McVites", 
 
            date:"2014-09-05T17:57:28.556094Z" 
 
           }, 
 
           { 
 
            rating:3, 
 
            comment:"Eat it, just eat it!", 
 
            author:"Michael Jaikishan", 
 
            date:"2015-02-13T17:57:28.556094Z" 
 
           }, 
 
           { 
 
            rating:4, 
 
            comment:"Ultimate, Reaching for the stars!", 
 
            author:"Ringo Starry", 
 
            date:"2013-12-02T17:57:28.556094Z" 
 
           }, 
 
           { 
 
            rating:2, 
 
            comment:"It's your birthday, we're gonna party!", 
 
            author:"25 Cent", 
 
            date:"2011-12-02T17:57:28.556094Z" 
 
           } 
 
           
 
          ] 
 
        }; 
 
      
 
      $scope.dish = dish; 
 
      
 
     }]) 
 

 
     .controller('DishCommentController', ['$scope', function($scope) { 
 

 

 
      $scope.newcomment = { 
 
       rating : "", 
 
       comment: "", 
 
       author: "", 
 
       date: new Date().toISOString() 
 
     }; 
 
      $scope.submitComment = function() { 
 
       
 
       //Step 2: This is how you record the date 
 

 

 
       $scope.dish.comments.push($scope.newcomment); 
 
       
 
       
 
      } 
 
     }]) 
 

 
;
<!DOCTYPE html> 
 
<html lang="en" ng-app="confusionApp"> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    
 
     <link href="../bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"> 
 
    <link href="../bower_components/bootstrap/dist/css/bootstrap-theme.min.css" rel="stylesheet"> 
 
    <link href="../bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet"> 
 
    <link href="styles/bootstrap-social.css" rel="stylesheet"> 
 
    <link href="styles/mystyles.css" rel="stylesheet"> 
 
<!-- endbuild --> 
 

 
    
 
     <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> 
 
     <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> 
 
    <![endif]--> 
 

 
</head> 
 

 
<body> 
 

 
    <div class="container"> 
 
     <div class="row row-content" ng-controller="DishDetailController"> 
 
      <div class="media" ng-model="dish"> 
 
       <div class="media-left media-middle"> 
 
        <a href="#"> 
 
         <img class="media-object img-thumbnail" ng-src={{dish.image}} alt="Uthapizza"> 
 
        </a> 
 
       </div> 
 
       <div class="media-body"> 
 
        <h2 class="media-heading"> {{dish.name}} 
 
         <span class="label label-danger"> 
 
      {{dish.label}} 
 
     </span> 
 
         <span class="badge"> 
 
      {{dish.price | currency}} 
 
     </span> 
 
        </h2> 
 
        <p>{{dish.description}}</p> 
 
       </div> 
 

 
      </div> 
 
     </div> 
 
     <br> 
 
     <br> 
 
     <br> 
 

 
     <div class="col-xs-9 col-xs-offset-1" ng-controller="DishDetailController"> 
 
      <h4>Customer Comments</h4> Sort by: 
 
      <input type="text" ng-model="filterText"> 
 
      <blockquote ng-repeat="commentla in dish.comments | orderBy: filterText"> 
 
       <p class="mb-0"> {{commentla.rating}} Stars </p> 
 
       <p class="mb-0">{{commentla.comment}}</p> 
 
       <footer class="blockquote-footer"> {{commentla.author}} {{commentla.date | date: 'mediumDate'}} </footer> 
 
      </blockquote> 
 
     </div> 
 
    </div> 
 

 
      <div class="col-xs-9 col-xs-offset-1" ng-controller="DishCommentController"> 
 
        <ul class="list-unstyled"> 
 
        Your Name: {{newcomment.author}} 
 
         <br> 
 
         Comment: {{newcomment.comment}} 
 
         <br> 
 
         Your Rating: {{newcomment.rating}} 
 

 

 
\t \t \t \t \t <!-- \t <p>Task 3: Here you include the code to show the live preview of the comment</p> 
 
\t \t \t \t \t \t <p>The comment should be shown only when the form contains valid 
 
\t \t \t \t \t \t information and is not pristine</p> --> 
 
        </ul> 
 
       <form class="form-horizontal" name="commentForm" ng-submit="submitComment()" novalidate> 
 
        <div class="form-group"> 
 
         <label for="yname" class="col-sm-2 control-label">Your Name:</label> 
 
         <div class="col-sm-10"> 
 
         <input type="text" class="form-control" id="yname" aria-des    <div class="col-sm-10"> 
 
         <label class="radio-inline"> 
 
          <input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="1" ng-model="newcomment.rating"> 1 
 
         </label> 
 
         <label class="radio-inline"> 
 
          <input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="2" ng-model="newcomment.rating"> 2 
 
         </label> 
 
         <label class="radio-inline"> 
 
          <input type="radio" name="inlineRadioOptions" id="inlineRadio3" value="3" ng-model="newcomment.rating"> 3 
 
         </label> 
 
         <label class="radio-inline"> 
 
          <input type="radio" name="inlineRadioOptions" id="inlineRadio4" value="4" ng-model="newcomment.rating"> 4 
 
         </label> 
 
         <label class="radio-inline"> 
 
          <input type="radio" name="inlineRadioOptions" id="inlineRadio5" value="5" ng-model="newcomment.rating"> 5 
 
         </label> 
 
         </div> 
 
        </div> 
 
        <div class="form-group"> 
 
         <label for="commentm" class="col-sm-2 control-label">Your Comment</label> 
 
         <div class="col-sm-10"> 
 
          <textarea class="form-control" id="commentm" name="commentm" rows="12" ng-model="newcomment.comment"></textarea> 
 
         </div> 
 
        </div> 
 
        <div class="form-group"> 
 
         <div class="col-sm-offset-2 col-sm-10"> 
 
          <button type="submit" class="btn btn-primary" ng-disabled="feedbackForm.$invalid">Submit Comment</button> 
 
         </div> 
 
         
 
        </div> 
 
       </form> 
 

 
     </div> 
 
    </div> 
 

 
<!-- build:js scripts/main.js --> 
 
    <script src="../bower_components/angular/angular.min.js"></script> 
 
    <script src="scripts/app.js"></script> 
 
<!-- endbuild --> 
 

 
</body> 
 

 
</html>

+1

このプロセスは、多くの場合、解決につながるとそうでないときにはお答えすることが容易になりますようMCVE](https://stackoverflow.com/help/mcve)にそれを煮詰めてみてください。 – WBT

+0

Angularでは、スコープはDOM階層を反映しているため、スコープはその上位スコープからプロパティを継承し、ルートスコープは最上位に継承されます。スコープは、子孫のいずれからも継承しません。発生すると予想される(現れる)兄弟から継承しません。 –

+0

ありがとうございます。私はこれをrootscopeとして作成しようとしました。その場合でも配列にプッシュしません。最初のスコープに2番目のスコープ追加機能を削除しました。私が何をしても、配列に新しい要素を追加することはありません。 –

答えて

0

をお読みいただきありがとうございました。サブミット機能がコントローラDishCommentControllerにある間にdish.commentsDishDetailControllerに定義しています。

も、その範囲内の$ scope.dish.commentsを持っていないので、DishCommentControllerDishDetailControllerコントローラにDishCommentControllerコントローラからsubmitcomment機能を取って試してみてください。

編集:提案としてあなたは、コードを更新した後、それを試してください

.controller('DishDetailController', ['$scope', function($scope) { 

    var $scope.dish ={ 
        name:'Uthapizza', 
        image: 'images/uthapizza.png', 
        category: 'mains', 
        label:'Hot', 
        price:'4.99', 
        description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.', 
        comments: [ 
         { 
          rating:5, 
          comment:"Imagine all the eatables, living in conFusion!", 
          author:"John Lemon", 
          date:"2012-10-16T17:57:28.556094Z" 
         } 
         ... 
        ] 
      }; 

    $scope.newcomment = { 
     rating : "", 
     comment: "", 
     author: "", 
     date: new Date().toISOString() 
    }; 

    $scope.submitComment = function (rating , author, stars, date) { 
     $scope.dish.comments.push($scope.newcomment);  
    } 

}]); 

を与え、私はそれが作業を開始したときに、後で戻って追加することができますいくつかの余分なコードを削除しました。

<div class="row row-content" ng-controller="DishDetailController">   

     <div class="col-xs-9 col-xs-offset-1"> 
      <h4>Customer Comments</h4> Sort by: 
      <input type="text" ng-model="filterText"> 
      <blockquote ng-repeat="commentla in dish.comments | orderBy: filterText"> 
       <p class="mb-0"> {{commentla.rating}} Stars </p> 
       <p class="mb-0">{{commentla.comment}}</p> 
       <footer class="blockquote-footer"> {{commentla.author}} {{commentla.date | date: 'mediumDate'}} </footer> 
      </blockquote> 
     </div>  

    <div class="col-xs-9 col-xs-offset-1"> 
      <ul class="list-unstyled"> 
      Your Name: {{newcomment.author}} 
       <br> 
       Comment: {{newcomment.comment}} 
       <br> 
       Your Rating: {{newcomment.rating}} 

      </ul> 
     <form class="form-horizontal" name="commentForm" ng-submit="submitComment()" novalidate> 
      <div class="form-group"> 
       <label for="yname" class="col-sm-2 control-label">Your Name:</label> 
       <div class="col-sm-10"> 
       <input type="text" class="form-control" id="yname" aria-des    <div class="col-sm-10"> 
       <label class="radio-inline"> 
        <input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="1" ng-model="newcomment.rating"> 1 
       </label> 
       .. 
      </div> 
      <div class="form-group"> 
       <label for="commentm" class="col-sm-2 control-label">Your Comment</label> 
       <div class="col-sm-10"> 
        <textarea class="form-control" id="commentm" name="commentm" rows="12" ng-model="newcomment.comment"></textarea> 
       </div> 
      </div> 
      <div class="form-group"> 
       <div class="col-sm-offset-2 col-sm-10"> 
        <button type="submit" class="btn btn-primary" ng-disabled="feedbackForm.$invalid">Submit Comment</button> 
       </div> 

      </div> 
     </form> 
    </div> 
</div> 
+0

コメントありがとうございます。私もそれを試みた。しかし、私は何をしているのですか:私はちょうどエラーが発生しました:未定義のプロパティ 'comments'を読み取ることができません –

+0

@AgilYolchuyevなぜ多くのngコントローラまたは単一の機能だけのコントローラを持っているのか分かりません –

+0

javascriptの部分を変更しました。 –

関連する問題