Home passing a parameter to a service using ngclick AngularJS
Reply: 1

passing a parameter to a service using ngclick AngularJS

Aneta Jabłońska
1#
Aneta Jabłońska Published in 2017-11-14 18:12:23Z

Hey I have array of numbers, I am writing them using ng-repeat. When you click on some number should be pass to function passParameterToService parameter index and should be saved in service to variable this.whichBoard. But I have do something wrong, can someone tell me why ng-repeat don't work when I add service and why after click on some number, it's dont write to a variable?

demo

https://codepen.io/Turqus/pen/WXEryN

rrd
2#
rrd Reply to 2017-11-14 18:41:14Z

In your service you were declaring this.whichBoard but inside the function you again used the same named variable. If you set this.board inside, it would probably work:

app.service('serwis', ()=> {
  this.whichBoard = (index) => {
    this.board = index;
  };
});

Also in the calling method, you have:

<a href="#" ng-click="passParameterToService($index)">
  <span ng-repeat="item in arrayIndex">{{item}}</span>
</a>

You're passing $index, which isn't a value. Also, you have the repeat inside the method call, so change it to:

<div ng-repeat="item in arrayIndex track by $index">
  <a href="#" ng-click="passParameterToService(item)">{{item}}</a>
</div>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.322182 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO