javascript - AngularJS 抛出未知错误

标签 javascript angularjs

我的代码是这样的

<body>
<div>
    <table ng-app='myApp'>
        <thead>

        </thead>
        <tbody ng-controller="MainCtrl">
            <tr ng-repeat="prdElement in palletElement">

                <td>
                    {{prdElement.name}}
                </td>

                <td>
                    {{prdElement.itemId}}
                </td>
                <td>
                    {{prdElement.shipmentId}}
                </td>
                <td>
                    {{prdElement.itemCode}}
                <td>
                    {{prdElement.description}}
                </td>

                <td>
                    {{prdElement.handlingUnit}}
                </td>
                <td>
                    {{prdElement.weight}}
                </td>
                <td>
                    {{prdElement.class}}
                </td>
                <td>
                    {{prdElement.lenght}}
                </td>

                <td>
                    {{prdElement.width}}
                </td>
                <td>
                    {{prdElement.height}}
                </td>
                <td>
                    {{prdElement.flag}}
                </td>

                <td>
                    <input type="text" ng-model="prdElement.quantity" placeholder=" Code" required />

                </td>
                <td>

                    <button ng-click="newPrdItem( prdElement,$event)">Submit</button>

                </td>
            </tr>
            <tr>

            </tr>
        </tbody>

    </table>
</div>

(function () {
    angular.module('myApp', []).controller('MainCtrl', function ($scope) {

        var counter = 0;

        $scope.palletElement = [{
            name: 'Pallet 1',
            itemId: '284307',
            shipmentId: 'eb44f690-c97a-40e3-be2a-0449559e171a',
            itemCode: '',
            description: 'Bicycle parts - frame',
            quantity: '31',
            handlingUnit: 'CTN',
            weight: '613.04',
            class:'',
            lenght: '102',
            width: '42',
            height: '61',
            flag:'P'

        }, {
            name: 'Pallet 2',
            itemId: '284308',
            shipmentId: 'eb44f690-c97a-40e3-be2a-0449559e171a',
            itemCode: '',
            description: 'Bicycle parts - fork',
            quantity: '22',
            handlingUnit: 'CTN',
            weight: '242.99',
            class: '',
            lenght: '75',
            width: '34',
            height: '18',
            flag: 'P'
        }]

        $scope.newPrdItem = function (prdElement, $event) {
            counter++;
            alert(prdElement.name);
            prdElement.push({
                name: prdElement.name,
                itemId: prdElement.itemId,
                shipmentId: prdElement.shipmentId,
                itemCode: prdElement.itemCode,
                description: prdElement.description,
                quantity: prdElement.quantity,
                handlingUnit: prdElement.handlingUnit,
                weight: prdElement.weight -10,
                class: prdElement.class,
                lenght: prdElement.lenght,
                width: prdElement.width,
                height: prdElement.height,
                flag: prdElement.flag,
            });

        }

    });
}());

对我来说一切看起来都不错,但这会在单击按钮时引发未知错误

TypeError: undefined is not a function at Scope.$scope.newPrdItem

Fiddle

最佳答案

您通过对 push 发出调用来将 prdElement 视为数组,但这实际上是从 View 传递的对象:

这条线是失败的。

prdElement.push({

我将创建一个单独的数组来跟踪被单击的项目

var items = [];
$scope.newPrdItem = function (prdElement, $event) {
            counter++;
            alert(prdElement.name);
            items.push({
                name: prdElement.name,
                itemId: prdElement.itemId,
                shipmentId: prdElement.shipmentId,
                itemCode: prdElement.itemCode,
                description: prdElement.description,
                quantity: prdElement.quantity,
                handlingUnit: prdElement.handlingUnit,
                weight: prdElement.weight -10,
                class: prdElement.class,
                lenght: prdElement.lenght,
                width: prdElement.width,
                height: prdElement.height,
                flag: prdElement.flag,
            });

        }

关于javascript - AngularJS 抛出未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26258112/

相关文章:

javascript - 更改图像的自然大小

java - GWT ScrollPanel 中使用 MouseWheel 进行水平滚动?

java - 在Java中解析包含JS的HTML页面

html - Angular.js 以旧方式提交表单

javascript - 在 Controller 中进行单元测试 $http 解析

angularjs - 通过 ng-click 选择选项在使用 AngularJS 的 chrome 浏览器中不起作用

javascript - 使用 moment.js (时区)时以 ("YYYY-MM-DD HH:mm") 格式返回日期和时间

javascript - Material-UI 样式 ThemeProvider 错误 : TypeError: Cannot read property 'primary' of undefined

c# - 从 C# MVC 模型设置 AngularJS $provide.constant 值的最佳方法是什么?

javascript - JSON 文件内容未在 HTML 中加载