I started learning angularjs in w3schools tutorials. Below was an example that was given to write your custom services
app.service('hexafy', function() {
this.myFunc = function (x) {
return x.toString(16);
}
});
app.controller('myCtrl', function($scope, hexafy) {
$scope.hex = hexafy.myFunc(255);
});
Why doesn't the below work -
app.service('hexafy', function(x) {
return x.toString(16);
});
app.controller('myCtrl', function($scope, hexafy) {
$scope.hex = hexafy(255);
});
Here I am letting the function accept an argument and processing it without creating a new function within it. But this approach doesn't seem to work.
.service(), you must pass a function that will be used to instantiate a new object. Here, that's not what you do, therefore, it does not work. BTW, w3schools is quite a weird place to start with AngularJS, you will probably find some better tutorials on the official website.