0

I created a service that has a method that does a simple calculation. It takes a value and squares it. However, I'm getting an error saying that square is undefined so I'm guessing I'm passing in the value incorrectly.

jsfid: http://jsfiddle.net/ADukg/19107/

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

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

/* function MyCtrl($scope) {
    $scope.name = 'Superhero';
} */

myApp.controller('MyCtrl', ['$scope', function($scope, CalcService) {
var cheese = CalcService.square(2);
  $scope.value = cheese;
  
}]);

myApp.service('CalcService', function(x){
   this.square = function(x) {

   var y = x*x;
      return y;
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="MyCtrl">
  Hello, {{value}}!
</div>

3
  • 2
    In your controller you aren't injecting the CalcService, and you're adding the parameter x to the service function. You should remove it. Check this updated jsFiddle: jsfiddle.net/ADukg/19114 Commented Feb 28, 2018 at 4:51
  • Ah ok, syntax error, so the injection happens in the beginning with the quotes, why the need to put it in the function param then? Commented Feb 28, 2018 at 4:59
  • This is for file minification. Check this post: stackoverflow.com/questions/18698963/…. You can write it like myApp.controller('MyCtrl', function($scope, CalcService) {} but this won't work when you minify your file. Commented Feb 28, 2018 at 5:02

4 Answers 4

1

i think you missed to inject service to controller in proper way check the snippet

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>

  <div ng-app="myApp">

  <div ng-controller="MyCtrl">
  Hello, {{value}}!
</div>
  </div>
</body>

</html>

<script>
var myApp = angular.module('myApp',[]);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

/* function MyCtrl($scope) {
    $scope.name = 'Superhero';
} */

myApp.service('CalcService', function(){
   this.square = function(x) {

   var y = x*x;
      return y;
   }
});
myApp.controller('MyCtrl', ['$scope','CalcService', function($scope, CalcService) {
var cheese = CalcService.square(2);
  $scope.value = cheese;
  
}]);


</script>

</body>

</html>

Sign up to request clarification or add additional context in comments.

Comments

1

See here you missed CalcService inject in controller initialization:

myApp.controller('MyCtrl', ['$scope', 'CalcService', function($scope, CalcService) {
var cheese = CalcService.square(2);
$scope.value = cheese;

}]);

Comments

1

You missed to inject 'CalcService'.

myApp.controller('MyCtrl', ['$scope','CalcService', function($scope, CalcService) { }]);

Comments

0

Your service should be something like:

myApp.service('CalcService', function(){
return {
    square: function (x) {
        var y = x*x;
        return y;
    }
}});

And you should inject service in your controller.

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.