1

I have a function in angular:

$scope.getSessionsForSpeaker = function (id) {
    $scope.id = id;
    $scope.getAll = SessionService.getAll();
    $scope.sessionsFound = [];
    $scope.getAll.$promise.then(function(data) {
        for (var i = 0; i < data.length; i++) {
            if ([data[i].speakers[0]] == $scope.id) {
                console.log([data[i].subject]);
                $scope.sessionsFound += [[data[i].subject] + ", "];
            }
        }
    });
};

and from html i run it by clicking a link:

<a ng-click="getSessionsForSpeaker(speaker.id)"></a>

Is there a way to run this function without clicking? Thank you in advance!!

5
  • 3
    Just call it - after declaration add $scope.getSessionsForSpeaker() Commented Dec 12, 2015 at 20:07
  • What should be speaker.id in that case ? Commented Dec 12, 2015 at 20:07
  • You can do plenty. Do you want to call it on some event or from your code? You want it to run on when controller/link function runs etc.? Commented Dec 12, 2015 at 20:12
  • @Alexander is right. And you need to pass an id . Commented Dec 12, 2015 at 20:14
  • Chris, I want it to run when my page loads, or when I access a certain controller. Commented Dec 12, 2015 at 20:29

2 Answers 2

1

You can run it from your controller if you know the ID:

$scope.getSessionsForSpeaker(id);

You can use ng-init to run it when that element loads:

<a ng-init="getSessionsForSpeaker(speaker.id)"></a>

You can also use ng-mouseover if you want to run it when the mouse is over that element

<a ng-mouseover="getSessionsForSpeaker(speaker.id)"></a>

All depends when you want to run it.

Sign up to request clarification or add additional context in comments.

3 Comments

What's the point in creating a $scope method if a given function will only run in constructor function and will not get exposed as view model property?
He can run it from anywhere he likes, I have no idea how he wants to run it, so I've presented him with every choice I could think of.
<a ng-init="getSessionsForSpeaker(speaker.id)"></a> doesn't seem to work.
0

I found a solution that worked for me:

<p ng-bind-html="sessionsFound"></p>

Thanks to everyone for contribution!

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.