gpt4 book ai didi

javascript - AngularJS 不重复数组

转载 作者:行者123 更新时间:2023-11-27 23:28:54 26 4
gpt4 key购买 nike

所以问题是 list.html 中的 ng-repeat 没有显示任何东西(或者它甚至没有得到任何东西?)。我还想知道我的 $scope.save 函数中的 if 语句是否是执行此操作的愚蠢方法(我的意思是将复选框 bool 值更改为"is"或“否”字符串)。

index.html

<!DOCTYPE html>
<html ng-app="app">
<body ng-controller="mainController">
<div ng-view></div>
</body>
</html>

表单.html

<form role="form">
<label>Nimi:</label>
<input ng-model="newcontact.name" type="text" class="form-control">
<label>Email:</label>
<input ng-model="newcontact.email" type="text" class="form-control">
<label>Ruokavalinta:</label>
<select ng-model="newcontact.ruoka" class="form-control">
<option>Kala</option>
<option>Liha</option>
<option>Kasvis</option>
</select>
<label><input ng-model="newcontact.sauna" type="checkbox"> Osallistun saunailtaan</label>
<button ng-click="save()" class="btn btn-default">Submit</button>
</form>

列表.html

<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>

应用程序.js

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 = {};
};
});

最佳答案

returnListdataservice的一个方法,不是数组类型的属性,所以你的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 = {};
};
});

关于javascript - AngularJS 不重复数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36959861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com