1

I am new to angular and trying to create a new custom service and pull data on view. What may be the issue here?

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="Scripts/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
    service data is {{serviceData}}
</body>
</html>


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

    app.controller('myCtrl', function ($scope, myService) {       
        $scope.serviceData = myService.getDetails;
    });

    app.factory('myService', function () {
        return
        {
            getDetails: "I am testing service....";
        };
    });

</script>

1 Answer 1

4

Please, remove semicolon from getDetails property declaration

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

app.controller('myCtrl', function($scope, myService) {
  $scope.serviceData = myService.getDetails;
});

app.factory('myService', function() {
  return {
    getDetails: "I am testing service...."
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">
  service data is {{serviceData}}
</div>

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

3 Comments

also important that object starts on same line as return
OH MY God.... you are right charlietfl. I am wondering how this is happening.... how will that make more difference if { is next to return or in next line ......?
@chandana What are the rules for JavaScript's automatic semicolon insertion – stackoverflow.com/questions/2846283/…

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.