UNPKG

ng-wedge

Version:

Intercept $http request from the website without modifying code or installing plugins.

44 lines (42 loc) 1.88 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>ng-wedge</title> <script src="node_modules/angular/angular.js"></script> <script src="node_modules/lazy-ass/index.js"></script> <script src="ng-wedge.js"></script> </head> <body ng-app="App" ng-controller="AppController"> <h1>ng-wedge</h1> <button id="load" ng-click="load()">Load using Ajax request</button> <p>load result "{{ loadResult }}"</p> <button id="installWedge">Install ng-wedge</button> <p class="instructions">Open browser console to see error messages. First try clicking on the "Load" button above. The page tries to execute http ajax call to non-existent url. Then install http intercept and mock response by clicking on "Install ng-wedge". Click "Load" button again and 2 seconds later you will see mock data appear.</p> <p>Code at <a href="https://github.com/bahmutov/ng-wedge">bahmutov/ng-wedge</a>, available on NPM and bower under <i>ng-wedge</i></p> <script> angular.module('App', []) .controller('AppController', function ($scope, $http) { $scope.load = function load() { $scope.loadResult = 'loading...'; $http.get('/some/url').then(function (data) { console.log('$http get /some/url returned', data); $scope.loadResult = data; }, function () { console.error('Could not get /some/url'); $scope.loadResult = 'Could not get /some/url'; }); }; }); </script> <script> document.querySelector('#installWedge').addEventListener('click', function () { console.log('installing ng wedge'); var mockHttp = wedge('#load', 'load'); mockHttp('get', '/some/url', 200, 'I got mock data back after 2 seconds', 2000); }); </script> </body> </html>