Lawrence Martin
Lawrence Martin

Reputation: 93

Return Value From function in controller to Page

I am passing a value from a page with ng-click to a function in a controller. This is successful, I am able to print the value to the console from the function. I am unable to access this value outside of the function, if I try to print outside of the function I get nothing. Specifically I want to display the value on another page. I am a beginner to ionic/angularjs, any help would be appreciated.

My code:

HTML

<a href="" ng-click="viewStory({{story.articleId}})">

Controller

.controller('NewsDisplayCtrl', function($scope, Articles) {  

    articleId = " ";
    text = " ";


    $scope.viewStory = (function(articleId){
    console.log("This is the passed in parameter " + articleId); 
        text = articleId;
    });

});

Next Page

<div ng-controller="NewsDisplayCtrl">      

    <h3>By: {{text}}</h3>

</div>

Upvotes: 1

Views: 541

Answers (2)

Kambo&#231;yalı
Kambo&#231;yalı

Reputation: 21

If you want to $broadcast use the $rootScope:

$rootScope.$broadcast('broadcast', { foo: {} });

And then receive them:

$scope.$on('broadcast', function(event, args) {

    var foo = args.foo;
    // Your Codes
});

Upvotes: 1

charlietfl
charlietfl

Reputation: 171679

Just remove the {{}} expression braces:

<a href="" ng-click="viewStory(story.articleId)">

Parameters passed to functions in ng- directives are evaluated as scope variables already

To share with another controller in another view you will need a service to share data and methods with

Upvotes: 0

Related Questions