Javascript AngularJS不重复数组
所以问题是,list.html中的ng repeat没有显示任何内容(或者甚至没有显示任何内容?)。我还想知道我的$scope.save函数中的if语句是否是这样做的愚蠢方式或否(我的意思是将复选框布尔值更改为“是”或“否”字符串) index.htmlJavascript AngularJS不重复数组,javascript,html,angularjs,Javascript,Html,Angularjs,所以问题是,list.html中的ng repeat没有显示任何内容(或者甚至没有显示任何内容?)。我还想知道我的$scope.save函数中的if语句是否是这样做的愚蠢方式或否(我的意思是将复选框布尔值更改为“是”或“否”字符串) index.html <!DOCTYPE html> <html ng-app="app"> <body ng-controller="mainController"> <div ng-view>
<!DOCTYPE html>
<html ng-app="app">
<body ng-controller="mainController">
<div ng-view></div>
</body>
</html>
returnList
是dataservice
的一种方法,不是数组类型的属性,因此您的mainController
应该用括号调用它:
app.controller("mainController", function($scope, dataservice){
$scope.list = dataservice.returnList();
$scope.save = function(){
if($scope.newcontact.sauna){
$scope.newcontact.sauna = "joo";
}else{
$scope.newcontact.sauna = "ei";
}
dataservice.save($scope.newcontact);
$scope.newcontact = {};
};
});
看看如何创建一个最小且可验证的示例:你们是如何如此快速地看到这些东西的?我每天要为这样的错误浪费2个小时。但是是的,非常感谢!
<table>
<tbody>
<tr ng-repeat="object in list">
<td>{{object.name}}</td>
<td>{{object.email}}</td>
<td>{{object.ruoka}}</td>
<td>{{object.sauna}}</td>
</tr>
</tbody>
</table>
var app = angular.module("app", ["ngRoute"]);
app.config(["$routeProvider", "$locationProvider",
function($routeProvider, $locationProvider){
var app = angular.module("app", ["ngRoute"]);
app.config(["$routeProvider", "$locationProvider",
function($routeProvider, $locationProvider){
$routeProvider
.when
("/list", {templateUrl: "harjoitus10/templates/list.html",
controller: "mainController"})
.when
("/form", {templateUrl: "harjoitus10/templates/form.html",
controller: "mainController"})
.otherwise({redirectTo: "/"});
$locationProvider.html5Mode({enabled:true, requireBase:false});
}]);
app.service("dataservice", function(){
var list = [{}];
this.save = function(newcontact){
list.push(newcontact);
};
this.returnList = function(){
return list;
};
});
app.controller("mainController", function($scope, dataservice){
$scope.list = dataservice.returnList;
$scope.save = function(){
if($scope.newcontact.sauna){
$scope.newcontact.sauna = "joo";
}else{
$scope.newcontact.sauna = "ei";
}
dataservice.save($scope.newcontact);
$scope.newcontact = {};
};
});
app.controller("mainController", function($scope, dataservice){
$scope.list = dataservice.returnList();
$scope.save = function(){
if($scope.newcontact.sauna){
$scope.newcontact.sauna = "joo";
}else{
$scope.newcontact.sauna = "ei";
}
dataservice.save($scope.newcontact);
$scope.newcontact = {};
};
});