【问题标题】:Accepting submissions [Angularjs]接受提交 [Angularjs]
【发布时间】:2015-06-18 02:54:26
【问题描述】:

我想用 angularjs 提交表单但没有工作任何人都可以找到我的错误在哪里:

我的表格:

 <form ng-controller="ReviewsController as reviewCtrl" ng-submit="reviewCtrl.addReview(product)">
                <blockquote >
                    <b>Stars: {{ reviewCtrl.review.stars }}</b>
                    {{ reviewCtrl.review.body }}
                    <cite>by: {{reciewsCtrl.review.author}}</cite>
                </blockquote>  
                    <select ng-model="reviewCtrl.review.stars">
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                    </select>
                    <input type="text" ng-model="reviewCtrl.review.body"/>
                    <input type="text" ng-model="reciewsCtrl.review.author"/>
                    <input type="submit"/>

                </form>

app.js

 //Reviews
    app.controller('ReviewsController' ,function(){
        this.review = {};

        this.addReview = function(product) {

            product.reviews.push(this.review);
        };
    });

我进入控制台的这个错误:

TypeError:无法读取未定义的属性“推送” 在 addReview (http://localhost/angular/js/app.js:107:25) 在 fn(评估在 (http://localhost/angular/js/angular.min.js:1:0), :4:353) 在 f (http://localhost/angular/js/angular.min.js:250:394) 在 m.$eval (http://localhost/angular/js/angular.min.js:134:83) 在 m.$apply (http://localhost/angular/js/angular.min.js:134:309) 在 HTMLFormElement。 (http://localhost/angular/js/angular.min.js:250:446) 在 HTMLFormElement.m.event.dispatch (https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js:3:8549) 在 HTMLFormElement.r.handle (https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js:3:5259)

【问题讨论】:

  • product 是一个字符串。它没有reviews 属性。
  • product 应该是什么?
  • 我不知道 - 它是你的应用程序。它应该是具有reviews 属性的值具有push 函数的东西。

标签: javascript angularjs html


【解决方案1】:

您将文字字符串('gems')传递给 addReview 方法,从您的代码来看,我认为该方法需要一个带有名为“reviews”的数组属性的“product”对象。

【讨论】:

  • 我需要将新产品添加到gems数组var gems = [{ reviews: [ { stars: 5, body: "Great product", author: "zamora@hotmail.rs" }, { stars: 3, body: "Bot bad", author: "v@hotmail.rs" } ] }, {....
猜你喜欢
  • 1970-01-01
  • 2017-09-22
  • 2011-04-03
  • 1970-01-01
  • 2015-06-02
  • 2023-02-07
  • 2016-03-05
  • 2016-02-19
相关资源
最近更新 更多