0

In a simple example, I am expecting console to ping me "USA". Instead I am getting an error. Can someone help fix it?

SNIPPET:

<html> 
<head> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> 
</head> 

<body ng-app="myApp" ng-controller="myCtrl"> 
<script>
//module declaration
var app = angular.module('myApp',[]);
//controller declaration
app.controller('myCtrl',function($scope, myFactory){
    var a = myFactory();
    console.log(a.country);
});

//factory declaration
app.factory('myFactory',function(){
    var obj = {};
    obj.country = "USA";
    return obj;
});
</script> 
</body> 
</html> 

Error:

enter image description here

0

5 Answers 5

1

try this

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> 


<body ng-app="myApp" ng-controller="myCtrl"> 
<script>
//module declaration
var app = angular.module('myApp',[]);
//controller declaration
app.controller('myCtrl',function($scope, myFactory){
    var a = myFactory;
    console.log(a.country);
});

app.factory('myFactory',function(){
    var obj = {};
    obj.country = "USA";
    return obj;
});
</script> 
</body> 
Sign up to request clarification or add additional context in comments.

Comments

0

As the runtime is telling you, myFactory is not a function.

var a = myFactory();

This call is therefore not valid. You can do this.

var a = myFactory;
console.log(a.country);

Or just:

console.log(myFactory.country);

Comments

0
    <html> 
<head> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> 
</head> 

<body ng-app="myApp" ng-controller="myCtrl"> 
<script>
//module declaration
var app = angular.module('myApp',[]);
//controller declaration
//factory declaration
app.factory('myFactory',function(){
    var obj = {};


    return {
        get: function(){
           obj.country = "USA";
            return obj;
        },

    }
});

app.controller('myCtrl',function($scope, myFactory){
    var a = myFactory.get();
    console.log(a.country);
});

</script> 
</body> 
</html> 

Comments

0

you can use the alternate way with $injector

//module declaration
var app = angular.module('myApp',[]);
//controller declaration
app.controller('myCtrl',function($scope, $injector){
    var a = $injector.get('myFactory')
    console.log(a.country);
});

//factory declaration
app.factory('myFactory',function(){
    var obj = {};
    obj.country = "USA";
    return obj;
});

Comments

0

You get the error because the factory returns a service object and you're trying to invoke it as a function.

Check the below code snippet.

angular
  .module('demo', [])
  .controller('DefaultController', DefaultController)
  .factory('demoFactory', demoFactory);

DefaultController.$inject = ['demoFactory'];

function DefaultController(demoFactory) {
  console.log(demoFactory.country);
}

function demoFactory() {
  var service = {
    country: 'USA'
  };
  
  return service;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demo">
  <div ng-controller="DefaultController as ctrl"></div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.