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 />
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With