Course Progress 54%

AngularJS API

AngularJS API का मतलब है AngularJS द्वारा दिए गए built-in objects, methods, services और utilities, जिनका उपयोग करके application को develop, control और manage किया जाता है। AngularJS API developers को framework के अंदर मौजूद ready-made functionality provide करता है, जिससे common tasks आसान हो जाते हैं।

AngularJS API को समझना इसलिए ज़रूरी है क्योंकि:

  • यही AngularJS का core power है
  • Controllers, services, directives और filters इन्हीं APIs पर depend करते हैं
  • Clean, scalable और maintainable code लिखा जा सकता है

AngularJS API के Main Parts

AngularJS API को broadly इन categories में divide किया जा सकता है:

  • Global API
  • Module API
  • Controller API
  • Service API
  • Directive API
  • Scope API
  • HTTP API
  • Utility API

अब इन्हें एक-एक करके समझते हैं।

AngularJS Global API

Global API वे methods हैं जो angular object के अंदर available होते हैं।

angular.module()

AngularJS application या module बनाने के लिए use होता है।

var app = angular.module("myApp", []);

Code Explanation

  • "myApp" module का नाम है
  • Empty array [] dependencies define करने के लिए है
  • यही AngularJS app की entry point होती है

angular.copy()

Object की deep copy बनाने के लिए use होता है।

$scope.original = { name: "Rahul" };
$scope.copyObj = angular.copy($scope.original);

Code Explanation

  • Original object unchanged रहता है
  • New object independent होता है
  • Forms reset और undo features में useful है

angular.equals()

दो objects को compare करने के लिए use होता है।

angular.equals(obj1, obj2);

Code Explanation

  • Deep comparison करता है
  • true या false return करता है
  • Data comparison logic में helpful है

Module API

Module API module के अंदर components define करने के लिए use होती है।

app.controller()

Controller define करने के लिए use होता है।

app.controller("myCtrl", function($scope) {
  $scope.message = "Hello API";
});

Code Explanation

  • Controller application logic handle करता है
  • $scope के जरिए data view तक जाता है

app.service()

Reusable service बनाने के लिए use होता है।

app.service("mathService", function() {
  this.add = function(a, b) {
    return a + b;
  };
});

Code Explanation

  • Service singleton होती है
  • Business logic centralize किया जाता है

app.factory()

Factory function return करता है।

app.factory("calcFactory", function() {
  return {
    multiply: function(a, b) {
      return a * b;
    }
  };
});

Code Explanation

  • Object return करता है
  • Flexible service creation के लिए use होता है

app.directive()

Custom directive बनाने के लिए use होता है।

app.directive("myText", function() {
  return {
    template: "<h3>Hello Directive</h3>"
  };
});
<my-text></my-text>

Code Explanation

  • Custom HTML behavior define करता है
  • Reusable UI components बनते हैं

Scope API

$scope AngularJS का core object है।

$scope.$watch()

Model changes observe करने के लिए use होता है।

$scope.$watch("name", function(newVal, oldVal) {
  $scope.info = "Value changed";
});

Code Explanation

  • Variable change detect करता है
  • Real-time reaction possible बनाता है

$scope.$apply()

AngularJS digest cycle manually trigger करने के लिए use होता है।

$scope.$apply(function() {
  $scope.value = 10;
});

Code Explanation

  • External events से model update करने में use होता है
  • AngularJS को changes detect करने में मदद करता है

HTTP API ($http)

Server से data fetch करने के लिए use होता है।

$http.get("api/users").then(function(response) {
  $scope.users = response.data;
});

Code Explanation

  • HTTP request send करता है
  • Promise return करता है
  • REST APIs consume करने के लिए standard तरीका है

$http Methods

  • $http.get() – data read
  • $http.post() – data insert
  • $http.put() – data update
  • $http.delete() – data delete

Filter API

AngularJS filters data transform करने के लिए use होते हैं।

<p>{{ name | uppercase }}</p>

Code Explanation

  • uppercase built-in filter है
  • Display-only transformation करता है

Utility API

AngularJS कई helper methods provide करता है।

angular.isString()

angular.isString("Hello");

Code Explanation

  • Data type check करता है
  • Validation logic में useful है

angular.isNumber()

angular.isNumber(100);

Code Explanation

  • Numeric value check करता है
  • Safe type checking देता है

Timeout API ($timeout)

AngularJS version of setTimeout.

$timeout(function() {
  $scope.msg = "Done";
}, 2000);

Code Explanation

  • Digest cycle के अंदर run होता है
  • UI update safe तरीके से होता है

Interval API ($interval)

AngularJS version of setInterval.

$interval(function() {
  $scope.count++;
}, 1000);

Code Explanation

  • Repeating task run करता है
  • Cleanup और cancel करना आसान होता है

Location API ($location)

URL और routing manage करने के लिए use होता है।

$location.path("/home");

Code Explanation

  • Browser URL change करता है
  • SPA navigation में use होता है

AngularJS API Best Practices

  • Built-in APIs को prefer करें
  • Same logic बार-बार न लिखें
  • Services में business logic रखें
  • Controllers lightweight रखें

Common API Mistakes

  • $scope.$apply() unnecessarily use करना
  • Direct DOM API call करना
  • Services की जगह controller में logic लिखना
  • $http error handling ignore करना

Summary

इस chapter में आपने सीखा:

  • AngularJS API क्या है
  • Global, Module और Scope APIs
  • $http, filters और utilities
  • Commonly used AngularJS APIs
  • Best practices और common mistakes