Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return value in angularjs service

Tags:

angularjs

I have two cases of using service return value in AngularJS.

Case 1. If I am using below code its working fine

var SerFactProv = angular.module("SerFactProv", [])
.service('ServiceDemo', function () {
    return { fn: "1452522" };
})
.controller("sfpCtrl", function ($scope, ServiceDemo, FactoryDemo, ProviderDemo) {
    $scope.serviceResult = "This is form service : " + ServiceDemo.fn
});

HTML:

<body ng-app="SerFactProv">
    <div ng-controller="sfpCtrl">
        <p>{{serviceResult}}</p>    <br />
    </div>
</body>

Case 2. But if I use .fn in angular expression in place of controller output disappears. Please see difference of .fn in both codes and explain why its happening.

$scope.serviceResult = "This is form service : " + ServiceDemo

and on UI

<p> {{serviceResult.fn}} </p>    <br />
like image 440
MukulSharma Avatar asked May 08 '26 16:05

MukulSharma


1 Answers

This code

$scope.serviceResult = "This is form service : " + ServiceDemo

and HTML:

<p> {{serviceResult.fn}} </p>

don't make any sense.

ServiceDemo is an object. Concatenation of the string and anything is going to be string too. Arbitrary string doens't have fn property. Hence the undefined result.

like image 150
dfsq Avatar answered May 10 '26 11:05

dfsq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!