javascript - Uncaught ReferenceError: Factory is not defined -
i trying set sample angular.js project mvc.
i had project returning message controller. when tried read webapi factory getting following error @ run time.
angulartutorial?v=exngsrzkujrjharmvxi_00qqlywtra533io1lciemd81:1 uncaught referenceerror: homefactory not defined(anonymous function) @ angulartutorial?v=exngsrzkujrjharmvxi_00qqlywtra533io1lciemd81:1 angular.js:11655 error: [$injector:unpr] unknown provider: homefactoryprovider <- homefactory <- homecontroller http://errors.angularjs.org/1.3.15/$injector/unpr?p0=homefactoryprovider%20%3c-%20homefactory%20%3c-%20homecontroller @ regex_string_regexp (angular.js:63) @ angular.js:4015 @ object.getservice [as get] (angular.js:4162) @ angular.js:4020 @ getservice (angular.js:4162) @ object.invoke (angular.js:4194) @ $get.extend.instance (angular.js:8493) @ angular.js:7739 @ foreach (angular.js:331) @ nodelinkfn (angular.js:7738)(anonymous function) @ angular.js:11655$get @ angular.js:8596$get.scope.$apply @ angular.js:14567bootstrapapply @ angular.js:1455invoke @ angular.js:4203dobootstrap @ angular.js:1453bootstrap @ angular.js:1473angularinit @ angular.js:1367(anonymous function) @ angular.js:26304trigger @ angular.js:2762eventhandler @ angular.js:3032
here files seem causing issue.
angulartutorial.js
var angulartutorial = angular.module('angulartutorial', []); angulartutorial.controller('homecontroller', homecontroller); angulartutorial.factory('homefactory', homefactory);
homefactory.js
var homefactory = function($http, $q) { return function() { var deferredresult = $q.defer(); $http.get('http://localhost:52506/api/message/get') .success(function(data) { deferredresult.resolve(data); }); return deferredresult.promise; } } homefactory.$inject = ['$http', '$q'];
homecontroller
var homecontroller = function ($scope, homefactory) { homefactory().then(function(response) { $scope.message = response; }); } homecontroller.$inject = ['$scope', 'homefactory'];
i have included link entire solution if help.
see below sample
var angulartutorial = angular.module('angulartutorial', []); angulartutorial.factory('homefactory', [ '$http', '$q', function($http, $q) { return function() { return $http.get('http://localhost:52506/api/message/get'); } } ]); angulartutorial.controller('homecontroller', [ '$scope', 'homefactory', function($scope, homefactory) { homefactory().then(function(response) { $scope.message = response; }); } ]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="angulartutorial"> <div ng-controller="homecontroller"> {{message}} </div> </div>
there no need of creating deferred
object in factory. $http.get
return promise so, can return $http.get(/*url*/)
.
Comments
Post a Comment