Reputation: 93
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
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
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