0

尝试使用 splice 删除对象时出现错误:TypeError: Object # has no method 'splice'

的结果console.log($scope.posts[$routeParams.id]);

Object {name: "test", url: "http://google.com", $$hashKey: "005"} 

我的添加和编辑功能都按预期工作。remove 是相似的,所以我希望它也能正常工作。

控制器.js

    /* Controllers */

hackerNews.controller('AppCtrl',
  function AppCtrl ($scope, posts, angularFire) {
      angularFire(posts.limit(100), $scope, 'posts');
  });

hackerNews.controller('InfoCtrl',
  function InfoCtrl($scope, $routeParams) {
    $scope.post = $scope.posts[$routeParams.id];
  });

hackerNews.controller('AddCtrl',
  function AddCtrl($scope, $location, posts) {
    $scope.post = [{}];
    $scope.add = function () {
      posts.push($scope.post);
      $location.url('/');
    };
  });

hackerNews.controller('EditCtrl',
  function EditCtrl($scope, $routeParams, $location, posts) {
    $scope.post = $scope.posts[$routeParams.id];
    $scope.edit = function () {
      $scope.posts[$routeParams.id] = $scope.post;
      $location.url('/');
    };
  });

hackerNews.controller('RemoveCtrl',
  function RemoveCtrl($scope, $routeParams, $location, posts) {
    $scope.post = $scope.posts[$routeParams.id];
    $scope.remove = function () {
      console.log($scope.posts[$routeParams.id]);
      $scope.posts.splice($routeParams.id, 1);
      $scope.toRemove = null;
      $location.url('/');
    };
    $scope.back = function () {
      $location.url('/');
    };
  });

应用程序.js

/* Declare app level module */
var hackerNews = angular.module('hackerNews', ['firebase'])
  .factory('posts', [function() {
      var posts = new Firebase('https://xyclos.firebaseio.com/hackerNews');
      return posts;
  }])
  .config(function($routeProvider) {
    $routeProvider.when('/index', {
      templateUrl: 'partials/index.html'
    })
    .when('/info/:id', {
      templateUrl: 'partials/info.html',
      controller: 'InfoCtrl'
    })
    .when('/add', {
      templateUrl: 'partials/add.html',
      controller: 'AddCtrl'
    })
    .when('/edit/:id', {
      templateUrl: 'partials/edit.html',
      controller: 'EditCtrl'
    })
    .when('/remove/:id', {
      templateUrl: 'partials/remove.html',
      controller: 'RemoveCtrl'
    })
    .when('/comments/:id', {
        templateUrl: 'partials/comments.html',
        controller: 'CommentCtrl'
    })
    .otherwise({
      redirectTo: '/index'
    });
  });

  hackerNews.filter('shortURL', function () {
      return function (text) {
          var getLocation = function(href) {
              var l = document.createElement("a");
              l.href = href;
              return l;
          };
          var url = getLocation(text);
          return url.hostname;
      };
  });

删除.html

<p>Are you sure that you want to remove {{post.name}}?</p>
  <button ng-click="remove()" class="btn btn-success btn-sm">Yes</button>
  <button ng-click="back()" class="btn btn-danger btn-sm">No</button>
4

1 回答 1

0

我让它工作,即使我不确定这是否一定是最好的方法。

这是我改变的:

删除.html

<button **ng-click="posts.remove(post)"** class="btn btn-success btn-sm">Yes</button>

之后,我不需要在 remove() 中做任何事情,除了路由回索引

$scope.remove = function () {
      $location.url('/');
    };
于 2013-09-12T13:37:24.297 回答