简体   繁体   English

角度推入数组给出:TypeError:无法读取未定义的属性“ push”

[英]Angular pushing into an array gives: TypeError: Cannot read property 'push' of undefined

I am trying to push the details from textbox to an array in AngularJS. 我正在尝试将详细信息从文本框推送到AngularJS中的数组。 But it doesn't work 但这不起作用

AppName.factory('sampleFactory', function(){
        var customerdetails=[
                {name:'John Smith', country:'Denmark', worth:'5000000'},
                {name:'John Lewis',country:'England',worth:'10000000'},
                {name:'Rick Evans',country:'America',worth:'6000000'}
            ];

        var factory={};
        factory.getCustomers=function(){
            return customerdetails;
        };

        return factory;
    });

AppName.controller('homeController', function($scope, sampleFactory){
    $scope.customers= sampleFactory.getCustomers();

    $scope.addCustomer = function(){
        $scope.customerdetails.push({
            name:$scope.customerName, 
            country:$scope.customerCountry,
            worth:$scope.customerWorth
        });
    };
});

The page has a button with ng-click="addCustomer". 该页面具有一个带有ng-click =“ addCustomer”的按钮。 But it doesn't add to the array. 但是它不会添加到数组中。

The factory part works fine. 工厂部分工作正常。 I am sure I have made an error in the addCustomer function. 我确定我在addCustomer函数中犯了一个错误。 Can somebody spot it? 有人可以发现吗? Thanks! 谢谢!

You are using the wrong variable name, it should be $scope.customers : 您使用了错误的变量名,应该是$scope.customers

$scope.addCustomer = function(){
        $scope.customers.push({
            name:$scope.customerName, 
            country:$scope.customerCountry,
            worth:$scope.customerWorth
        });
    };

You mistook it for the variable name of the service. 您将其误认为服务的变量名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 angular.js:14110 TypeError:无法读取undefined的属性'push' - angular.js:14110 TypeError: Cannot read property 'push' of undefined TypeError:无法读取二维数组中未定义的属性“推” - TypeError: Cannot Read Property 'push' of undefined in two-dimensional array 向数组React添加一个对象; “ TypeError:无法读取未定义的属性'push'” - Adding an object to the array React; “TypeError: Cannot read property 'push' of undefined” 数组:未捕获的类型错误:无法读取未定义的属性“推送” - Array: Uncaught TypeError: Cannot read property 'push' of undefined ReactJS:未处理的拒绝(TypeError):无法读取未定义的属性“推送”。 推送到另一个组件时遇到问题 - ReactJS: Unhandled Rejection (TypeError): Cannot read property 'push' of undefined. Having issues pushing to another component TypeScript:TypeError:无法读取未定义的属性“ push” - TypeScript: TypeError: Cannot read property 'push' of undefined “TypeError:无法读取未定义的属性‘push’”Vuejs - “TypeError: Cannot read property 'push' of undefined” Vuejs TypeError:无法读取nodejs中未定义的属性“ push” - TypeError: Cannot read property 'push' of undefined in nodejs TypeError:无法读取未定义的属性'push'-AngularJS - TypeError: Cannot read property 'push' of undefined - AngularJS TypeError:无法读取未定义的JavaScript属性'push' - TypeError: Cannot read property 'push' of undefined, JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM