[英]Remove JSON object in Angular
I have a file (courses.json) that I want to remove courses from when I click on the 'x' next to the course-name.我有一个文件 (courses.json),当我单击课程名称旁边的“x”时,我想从中删除课程。 I am very much a beginner at this, and I can't really get it to work.I have no problem reading from the file, but nothing happens when I click the 'x'.
我是这方面的初学者,我无法真正让它工作。我从文件中读取没有问题,但是当我单击“x”时没有任何反应。 Very grateful for all the help I can get!
非常感谢我能得到的所有帮助!
This is my code:这是我的代码:
var app = angular.module('myApp', []);
app.controller('courses', function($scope, $http) {
$http.get("courses.json").success(function(data) {
$scope.courses = data.kurser;
});
});
function courses($scope, courses) {
$scope.deleteItem = function (key) {
delete $scope.courses[key];
}
}
HTML: HTML:
<div ng-app="myApp">
<ul ng-controller="courses">
<li ng-repeat="(key, value) in courses" id="course-{{value.courseId}}">
<a href="#" class="courseIcon">{{value.courseName}}</a> <a ng-click="deleteItem(key)">x</a>
</li>
</ul>
</div>
You should define deleteItem
method in the same controller where you load data, otherwise courses
function is not linked to the application anyhow:您应该在加载数据的同一个控制器中定义
deleteItem
方法,否则courses
功能无论如何都不会链接到应用程序:
app.controller('courses', function($scope, $http) {
$http.get("courses.json").success(function(data) {
$scope.courses = data.kurser;
});
$scope.deleteItem = function (key) {
delete $scope.courses[key];
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.