繁体   English   中英

为什么在调用 $http 服务方法后重新加载页面?

[英]Why the page is reloaded after call $http service method?

我的 angularJS 应用程序中有 2 个控制器来模拟 CRUD 应用程序。 第一个控制器包含输入表单,第二个控制器是显示表,它们通过使用服务相互交换数据。 当我执行 CRUD 操作时,该服务将向 api 发送请求以更新数据(我使用JSON-Server https://github.com/typicode/json-server#getting-started作为虚拟休息 api 服务器)什么意外是当调用 $http 方法时,它导致页面重新加载,该页面不再是异步的,应该在 angularJS 模块中。

我的代码有什么问题? 我已经搜索了很多,但仍然没有找到任何解决方案。 也许我的搜索技巧不好,所以请有人帮助我

这是我的学生服务

app.service("studentService", function ($http, $filter) {
let student = {};
let students = [];

student.get = function () {
    return student;
};

student.set = function (sv) {
    student = sv;
};

student.create = function (data) {
    $http.post("http://localhost:3000/posts", data).then(function (response) {
        student = response.data;
    }, function (error) {
        alert("Create student failed");
    });
}

student.update = function (id, data) {
    $http.put("http://localhost:3000/posts/" + id, data).then(function (response) {
        student = response.data;
    }, function (error) {
        alert("Update student failed");
    });
}

student.delete = function (id) {
    $http.delete("http://localhost:3000/posts/" + id).then(function (response) {
        student = null;
    }, function(error) {
        alert("Delete student failed");
    });
}

students.get = function () {
    return students;
};

students.set = function () {
    $http.get("http://localhost:3000/posts", {cache: true}).then(function (response) {
        let data = $filter("orderBy")(response.data, "id");
        for (let sv of data) {
            students.push(sv);
        }
    });
}

return {
    student,
    students,
};

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM