Search code examples
angularjscrudrestful-urlangularjs-serviceangularjs-controller

No data returned in consuming REStful web service using Angularjs


enter image description hereI am beginner learning Angularjs .Please help me with examples for following

script added

javascript -

var app = angular.module('myapp', []);

app.controller('MyCtrl1', ['$scope', 'UserFactory', function ($scope, UserFactory) {
UserFactory.get({}, function (userFactory) {
    $scope.time = userFactory.time;
})
}]);
var service = angular.module('apiService', ['ngResource']);

service.factory('UserFactory', function ($resource) {
return $resource('http://time.jsontest.com', {}, {
    query: {
        method: 'GET',
        params: {},
        isArray: true
    }
})
});

.html file

<body ng-app="myapp">
<divng-controller="MyCtrl1" >
<p>
    Result from RESTful service is: {{ time }}
</p>
</div>
</body>

above snippet gives the out put as

Result from RESTful service is : {{time}}

and not the value i am expecting ..Reference : http://draptik.github.io/blog/2013/07/13/angularjs-example-using-a-java-restful-web-service/

I want to write CRUD methods (GET/POST/PUT/DELETE) and I have started with GET.

Thanks


Solution

  • You need to make sure that your main app module injects your service. In your plnkr you have:

    var app = angular.module('myapp', []);
    

    where you should really have:

    var app = angular.module('myapp', ['apiService']);
    

    This ensures that the service module is injected into your app module, and you can use the UserFactory that you define in that module. For this simple case you could have also simply defined the UserFactory factory on the 'myapp' module as well