简体   繁体   English

如何在angular js中更改位置并刷新页面

[英]How to change location and refresh page in angular js

I have a function : 我有一个功能:

$scope.insert = function(){
    var data = {
        'username' : $scope.username,
        'password' : $scope.password,
        'nama_lengkap' : $scope.nama_lengkap
    }

    $http({
        method: 'POST',
        url: './sys/mac.php',
        data : data
    }).then(function(response){
        return response.data;
    });
}

and the function work perfectly, but i want my page change to datalist and refresh datalist after insert() true. 和功能正常工作,但我希望我的页面更改为datalist并在insert()为true之后刷新datalist。 my function insert() run in the route "localhost/learn/#!/administrator" so i want it change to route "localhost/learn/#!/" after insert() true. 我的函数insert()在路由“ localhost / learn /#!/ administrator”中运行,因此我希望它在insert()true之后更改为路由“ localhost / learn /#!/”。 i used location.href='#!/' but it not work for refresh datalist automaticaly, just change location. 我使用location.href ='#!/'但不适用于自动刷新数据列表,仅更改位置。

If you want to update an object from a service call, you can do the following. 如果要通过服务调用更新对象,可以执行以下操作。 I have added an onError function too, to help with debugging. 我也添加了onError函数,以帮助调试。

Tip: Research adding service calls into a Service that AngularJS framework provides. 提示:研究将服务调用添加到AngularJS框架提供的Service中。 It helps for writing maintainable and structured code. 它有助于编写可维护的结构化代码。

$scope.objectToUpdate;
$scope.insert = function(){
    var data = {
        'username' : $scope.username,
        'password' : $scope.password,
        'nama_lengkap' : $scope.nama_lengkap
    }

    $http({
        method: 'POST',
        url: './sys/mac.php',
        data : data
    }).then(function(response){
        $scope.objectToUpdate = response.data.d;
    }, function(e){
        alert(e); //catch error
    });
   }

Optional Service 可选服务

Below is an example of how to make use of Angular Services to make server calls 以下是如何利用Angular Services进行服务器调用的示例

app.service('dataService', function ($http) {
    delete $http.defaults.headers.common['X-Requested-With'];
    this.getData = function (url, data) {
        // $http() returns a $promise that we can add handlers with .then() in controller
        return $http({
            method: 'POST',
            url: './sys/' + url + '.php',
            dataType: 'json',
            data: data,
            headers: { 'Content-Type': 'application/json; charset=utf-8' }
        });
    };
});

Then call this service from your controller, or any controller that injects DataService 然后从您的控制器或任何注入DataService控制器中调用此服务

var data = {
            'username' : $scope.username,
            'password' : $scope.password,
            'nama_lengkap' : $scope.nama_lengkap
        }
 dataService.getData('mac', data).then(function (e) {
    $scope.objectToUpdate = e.data.d;
 }, function (error) {
    alert(error);
 });

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

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