splice を使用してオブジェクトを削除しようとすると、エラーが発生します: TypeError: Object # has no method 'splice'
の結果console.log($scope.posts[$routeParams.id]);
Object {name: "test", url: "http://google.com", $$hashKey: "005"}
私の追加機能と編集機能は両方とも期待どおりに機能します。remove も同様なので、同様に機能することを期待しています。
controllers.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('/');
};
});
app.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;
};
});
remove.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>