Dimoreno
Dimoreno

Reputation: 227

Code javascript in AngularJS

Can I copy this code in my AngularJS APP. I'm new in AngularJS and I want to know. If I can copy this code done in javascript in my AngularJS APP

function callWS(url, type, dataType, data) {
    var resultado = '';
    $.ajax({
        url: url,
        type: type,
        dataType: dataType,
        contentType: "application/json; charset=utf-8",
        data: data,
        async: false,
        success: function(data) {
            resultado = data;
        },
        error: function(e, msg) {
            console.log(msg + ' en ws ' + url);
        }
    });

    return resultado;
}

Upvotes: 0

Views: 66

Answers (3)

Mahmoud
Mahmoud

Reputation: 874

If you want to use Angular, you don't need to use jquery functions. Use Restangular to handle Rest API Restful Resources. It's powerfull and easy: https://github.com/mgonto/restangular

Upvotes: 0

Hemant Kabra
Hemant Kabra

Reputation: 884

Yes, you can use the same code with the help of $http in angular JS:

See below code:

var myAngApp = angular.module('AngularApp', []);  
myAngApp.controller('customController', function ($scope, $http) {  
    $http({
        url: url,
        type: type,
        dataType: dataType,
        contentType: "application/json; charset=utf-8",
        data: data,
        async: false,
    }).success(function (data, status, headers, config) {  
        $scope.customers = data.d.results;  
    }).error(function (data, status, headers, config) {  
        });
});  

Hope this will help you..!!

Upvotes: 1

Gautam Patadiya
Gautam Patadiya

Reputation: 1412

Yes you can use the code of core JavaScript in AngularJS but some different rules and code style. Here ajax code you are going to copy but i suggest you '$http' service of AnuglarJS. For More information here:https://docs.angularjs.org/api/ng/service/$http

Upvotes: 0

Related Questions