【问题标题】:AngularJs TypeError: $scope.todos.push is not a functionAngularJs TypeError:$scope.todos.push 不是函数
【发布时间】:2017-06-25 07:28:49
【问题描述】:

我正在尝试使用 $http.get 方法将待办事项从我的控制器添加到我的数据库。但我收到错误“TypeError:$scope.todos.push 不是函数”。我已经查看了许多与我自己类似的问题,并尝试实施这些建议,但没有成功。问题出在 TodoController 的 $scope.createTodo 函数中。

todo.controller.js

 angular.module('app')

   .controller('TodoController', ['$scope', 'Todos', function TodoController($scope, Todos) {

    $scope.formData = {};
    console.log("in the TodoController");
    // when landing on the page, get all todos and show them
    Todos.get()
    .then(function (data){
        $scope.todos = data;
    }).then(function (error){

    });

    $scope.createTodo = function() {
        if(!$scope.todoForm.$valid) {
            return;
        }
        Todos.create($scope.formData)
        .then(function (data){
             $scope.formData = {};
             $scope.todos.push(data);
        }).then(function (error){

        });
    };

todo.service.js

angular.module('app')
.factory('Todos', ['$http', function($http) {

return {
    get: function() {
        return $http.get('/api/todos');

    },

    create: function(todoData) {
        return $http.get('/api/todos', todoData).then(function (success){

        },function (error){

        });

    },

    delete: function(id) {
        return $http.delete('/api/todos/' + id);
    },

    update: function(todoData) {
        return $http.put('/api/todos/' + todoData.id, todoData);
    }
}
}]);

【问题讨论】:

  • Todos.get().then(function (data){ ... }) 中放置一个断点,然后检查您分配给$scope.todos 的确切内容。我猜它不是一个数组。
  • 问题是您对todos 的初始分配-您应该在.then 响应中记录data 变量-它不是数组

标签: javascript angularjs http typeerror


【解决方案1】:

摆脱这种问题。

我只是首先检查不是'undefined' 来执行任何操作,如推送或长度

if($scope.todos != undefined){
    //to do here
} else {
    console.log("$scope.todos is undefined");
}

//or

if($scope.todos !== 'undefined'){
    //to do here
}

可以帮助有这个小问题的人。

【讨论】:

    【解决方案2】:

    因为你没有声明 $scope.todos 所以它返回 undefined 并且没有推送功能,我认为你必须先声明你的表:

    angular.module('app')
    
       .controller('TodoController', ['$scope', 'Todos', function TodoController($scope, Todos) {
        $scope.todos = [];
        $scope.formData = {};
        console.log("in the TodoController");
        // when landing on the page, get all todos and show them
        Todos.get()
        .then(function (data){
            $scope.todos = [data];
        }).then(function (error){
    
        });
    
        $scope.createTodo = function() {
            if(!$scope.todoForm.$valid) {
                return;
            }
            Todos.create($scope.formData)
            .then(function (data){
                 $scope.formData = {};
                 $scope.todos.push(data);
            }).then(function (error){
    
            });
        };
    

    【讨论】:

    • 我添加了 $scope.todos = [];到我的控制器,但仍然无法正常工作。谢谢
    • 你仍然有 TypeError: $scope.todos.push is not a function
    【解决方案3】:

    Push 仅适用于数组,将该模型转换为数组即可解决您的问题。

    【讨论】:

      猜你喜欢
      • 2018-09-05
      • 1970-01-01
      • 2017-05-17
      • 2023-04-01
      • 2012-10-23
      • 2016-07-12
      • 2015-11-23
      • 2015-03-13
      • 2015-09-21
      相关资源
      最近更新 更多