Can you build a mock website with Angularjs by just mocking all json responses from the server somehow?

38 views
Skip to first unread message

gitted

unread,
Feb 12, 2014, 10:31:31 AM2/12/14
to ang...@googlegroups.com
Say you want to start a new project, but you want a fully working website without having to do all the backend server side coding.

How could you do this with Angularjs?  Is it possible to route all requests to a mock backend which is just sample json responses?

This way, you can see how the website will work exactly like in production w/o having to write the database layer and backend serverices etc.

Martin Alix

unread,
Feb 12, 2014, 11:03:11 AM2/12/14
to ang...@googlegroups.com
Just use a service/factory...

Later on, you can swap $q for $http and call some RESTful backend...

You can also use $httpBackend, but I find that is more work at the onset...


angular.module('app')
  .factory('FooService', ['$q', function($q) {
    return {
      getFoo: function() {
        deferred = $q.defer();
        deferred.resolve({ "bar": "baz" });
        return deferred.promise;
      }
    };
  }])
  .controller('MyController', ['$scope', 'FooService', function($scope, fooService) {
    $scope.myBar = fooService.getFoo();
  }]); 
Reply all
Reply to author
Forward
0 new messages