dimanche 19 avril 2015

angular services declaration


Vote count:

0




I am working on a simple angular SPA. I got the services working, and then wanted to extract them to a separate file. I've done something wrong with syntax and dependencies, and I'm wondering if anyone can help me spot it. I know I am confusing some things, and there might be a couple of additional bugs. I'm open to hearing some ideas about where I'm going wrong here. Please advise.


app.js



'use strict';

var demoApp = angular.module('demoApp', [
'ngRoute',
'DemoControllers'
])
.service('DemoServices');

demoApp.config(['$routeProvider',
function($routeProvider){
$routeProvider.when('/products/', {
templateUrl: 'product-list.html',
controller: 'ProductListCtrl'
})
.when('/products/:product_id', {
templateUrl: 'product-detail.html',
controller: 'ProductDetailCtrl'
})
.otherwise({
redirectTo: '/products/'
});
}]);


controllers.js


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


DemoControllers.controller('ProductListCtrl', ['$scope', '$http', 'FetchList', function ($scope, $http, FetchList) { $scope.products = FetchList; }]);



DemoControllers.controller('ProductDetailCtrl', ['$scope', '$http', '$routeParams', 'FetchProduct',
function ($scope, $http, $routeParams, FetchProduct) {
$scope.selection = FetchProduct;
}]);


services.js:



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

DemoServices.factory('FetchList', ['$http', function ($http) {
var list;
var FetchList = function ($http) {
$http.get('http://ift.tt/1zxD36N')
.success(function (data, status, headers, config) {
// console.log('data :: ', data, 'status :: ', status, 'headers :: ', headers, 'config :: ', config);
list = data.products;
});
};
return {products: new FetchList($http)};
}]);

DemoServices.factory('FetchProduct', ['$http', '$scope', function ($http, $scope) {
var product;
var FetchProduct = function ($http, $scope) {
$http.get('http://ift.tt/1HHPG66' + $scope.product_id + '/detail')
.success(function (data, status, headers, config) {
console.log('data :: ', data, 'status :: ', status, 'headers :: ', headers, 'config :: ', config);
product = data;
});
};

return {product: new FetchProduct($http, $scope)};
}]);


asked 12 secs ago







angular services declaration

Aucun commentaire:

Enregistrer un commentaire