How can I get a single array lenght?


#1

Hello guys, I want to display the number of the array, separately, like:

1
2
3
4
etc…

But I’m getting only the total length, like number 10.


#2

Can anyone help me solve this? Thanks.


#3

What do you mean by displaying?

If you mean render them in html, using ng-repeat will solve your problem.

<div ng-repeat="num in YourArray">
  {{$index+1}}
  {{num.property}}
</div>

ng-repeat is an angular directive, visit official ng-repeat for more detailed introduction.

The $index above will be the absolute position of the element in the array, “$index+1” may be the thing you want to display.

The “num” is a renamable (call it anything you want in ngrepeat) iteration of every object in the array, you can use it just like YourArray[$index].

Hope this helps.


#4

Thanks Windht, but what I’m trying to do is to get the post title using the social sharing, like:

<button onclick="window.plugins.socialsharing.share(post.title)">message only</button>

I don’t now how to make it work…


#5

Well I checked the github page of socialsharing, this function is indeed passing in an string to work, assume you have all the setup done, I suggest you to wrap the function in an angular way.

By angular way, meaning using “ng-click” rather than “onclick”,

So change your html to:

<button ng-click="share(post.title)">
  Share
</button>

Then in your controller (I suggest you know what is $scope):

$scope.share=function(title){
   window.plugins.socialsharing.share(title);
}

Indeed, ionic team has done a wrap up of the social sharing function for angular at Social Sharing ngCordova. Do check it out because it is a more standard way of doing so in an ionic app.

Using angular meaning that scope isolation is something you have to consider all the time. So your old way may not be able to correctly passing in the “post.title”, feel free to ask if you have any more problems on this.