gpt4 book ai didi

javascript - 错误: Birthday.搜索不是函数(AngularJS)

转载 作者:行者123 更新时间:2023-11-28 07:19:43 24 4
gpt4 key购买 nike

我正在用 html、AngularJS 和 REST 编写一个 Web 应用程序(其余部分为我的老师修改)。我的程序应该在开头的选择中加载参与者。但显示的是 {{participants.name}} 而不是 {{Jhon}}

目前的问题是加载方法“search”。

错误:

"Error: Birthday.search is not a function $scope.updateList@localhost:9000/birthday.js:26:1 @localhost:9000/birthday.js:31:5 e@localhost:9000/node_modules/angular/angular.min.js:36:313 Fe/this.$get

HTML:

<!DOCTYPE html>
<html lang="es" data-ng-app="birthdayApp">
<html>
<head>
<meta charset="utf-8">
<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="node_modules/angular/angular.min.js"></script>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="css/estilo.css">
<script src="birthday.js"></script>
</head>
[...]
<body>
<div class="container-fluid" data-ng-controller="BirthdayControllers as birthday">
[...]
<select size="2" class="col-lg-12 col-md-12 col-xs-12">
<option data-ng-repeat="participant in participants | filter:filter"> {{participant.name}} </option>
</select>
[...]

头还好吗?

AngularJS

'use strict';
var app = angular.module("birthdayApp", [])

app.factory('Birthday', function($http) {
return function(errorHandler) {
this.search = function(callback, errorHandler) {
$http.get('/participants').success(callback).catch(errorHandler);
}
}
});

/* Controllers */
app.controller("BirthdayControllers", function($scope, Birthday) {
$scope.participants = null;

var errorHandler = function(error) {
$scope.notificarError(error.data);
};

$scope.updateList = function() {
Birthday.search(function(data) { //here stop, here the problem!
$scope.participants = data;
}, errorHandler);
}

$scope.updateList();
[...]

});

最佳答案

我的工厂看起来不一样。这是我的做法:

app.factory('Birthday', function ($http) {    
return {
search: function() {
return $http.get('/participants');
}
};
});

Controller 看起来像这样:

Birthday.search().success(function(data) {
$scope.participants = data;
}).error(function(data, status, headers, config) {
//handle the error
});

关于javascript - 错误: Birthday.搜索不是函数(AngularJS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30446524/

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