Reputation: 329
I have a function inside controller:
$scope.passValues = function (param1){
return "foo";
};
console.log($scope.passValues());
this will log foo
, but:
$scope.passValues = function (param1){
return param1;
};
console.log($scope.passValues());
this I don't understand why because:
$scope.passValues = function (param1){
console.log(param1);
return param1;
};
will log robert
, which is coorect when used as passValues(item.firstName)
.
My problem is with second example, where I need the function to correctly return param1 value so it can be used later in controller.
Upvotes: 0
Views: 6628
Reputation: 42113
I have came to this question when I was trying to search for possible reasons of scope()
to return undefined...
in my case, I had a module myApp
with MyController
with $scope.reloadItems
function:
<div ng-app='myApp' ng-controller='MyController' id="myAngularApp">
js files that I was loading for this:
angular.min.js
, ng-infinite-scroll.min.js
my_angular_app.js
with definition of my moduleand some_view.js
where I was doing:
$('#myAngularApp').scope().reloadItems();
Now the scope()
often returns undefined
when module can not be instantiated or there is some error. In this case it was because of order, in which I loaded .js files, because of which:
$('#myAngularApp').scope()
was executed before the my_angular_app.js
had a chance to build this module...
... so in case someone else comes here like me, I hope this will help :)
Upvotes: 0
Reputation: 329
not sure why the downgrade this was a legitimate question and here is the answer, I think I got misunderstood or gave bad explanation.
I had to use a service, to use param1 in another controller:
first controller:
$scope.passValues = function (param1){
myservice.firstName = param1;
};
the service:
.service('myservice', function() {
})
The second controller:
.controller('detail',
function ($scope, myservice) {
$scope.content = myservice.firstName;
});
then on the template, {{content}} correctly displays value of param1 (firstName).
Upvotes: 0
Reputation: 2354
Make sure to pass a value to your function, otherwise the argument will be undefined
from controller
$scope.passValues('Robert')
And from your view
<div>
{{ passValues('Robert') }}
</div>
Upvotes: 3