Use Services in AngularJS -
i trying make rest request angularjs.
main.js
angular.module('frontendapp') .controller('mainctrl', ['$scope', '$http', function($scope, $http) { $scope.loaddata = function() { $scope.services = services.query(); };
services.js
'use strict'; angular.module('frontendapp').factory('services', function($resource) { return $resource('/services/:serviceid', { serviceid: '@_id' }, {}); });
i error: services not defined. did wrong ?
edit: mains.html
<div class="jumbotron"> choose category1 <div class="wrap" ng-controller="mainctrl"> <select ng-model="selectedvalue" ng-change="loaddata()" > <option value="1">category 1</option> <option value="2">category 2</option> <option value="3">category 3</option> <option value="4">category 4</option> <option value="5">category 5</option> </select> <div ng-show="selectedvalue != null" class="main"> <center><h2>results category {{ selectedvalue }}</h2></center> <br><br> </div> <div class="main" > <ul class="cloudlist"> <li class="service" ng-repeat="item in services" ng-click="select(item)"> <div class="info"> <h3>{{item.service_name}}</h3> <b>{{item.status_page}}</b><br> <b>is billed : {{item.is_billed.billing_term._identifier}}</b> </div> </li> </ul> <br> </div> </div>
after injecting service in controller
angular.module('frontendapp') .controller('mainctrl', ['$scope', '$http', 'services', function ($scope, $http, services) { $scope.loaddata = function () { $scope.services = services.query(); };
i error: unknown provider: servicesprovider <- services <- mainctrl
you have inject services in controller.
var module = angular.module('frontendapp', ['ngresource']); module.factory('myservices', ['$resource', function($resource) { return $resource('/services/:serviceid', { serviceid: '@_id' }, {}); }]); module.controller('mainctrl', ['$scope', '$http','myservices', function ($scope, $http, myservices) { $scope.loaddata = function () { $scope.services = myservices.query(); }; }]);
Comments
Post a Comment