How to make AJAX call with angular2(ts)?

前端 未结 4 2057
春和景丽
春和景丽 2020-12-15 07:04

How to make AJAX call with angular2(ts)? I read the tutorial on angularjs.org. But there is nothing about AJAX. So I really want to know how to make AJAX call with angular

4条回答
  •  时光说笑
    2020-12-15 07:11

    AJAX is fully transparent in angularjs, see the links and examples below.

    https://docs.angularjs.org/api/ng/service/$http

    $http({
      method: 'GET',
      url: '/someUrl'
    }).then(function successCallback(response) {
        // this callback will be called asynchronously
        // when the response is available
      }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });
    

    https://docs.angularjs.org/api/ngResource/service/$resource

    var User = $resource('/user/:userId', {userId:'@id'});
    User.get({userId:123}, function(user) {
      user.abc = true;
      user.$save();
    });
    

提交回复
热议问题