0

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.

3 Answers 3

3

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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).

Comments

0

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 my_angular_app.js with definition of my module
  • and 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 :)

Comments

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.