Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a json String coming in which looks like:

"Action" : "0876"

I am getting this using the expression

{{submenu1.Action}}

What I want can be summarised as :

  • Step 1: Parse and get the final element "6" from "0876" into a variable substr
  • Step 2: Casting/converting this substr into a number
  • Step 3: Use this as a pointer like Topmenu.number

I am looking for a solution specific to AngularJS. Any help is appreciated.

share|improve this question
    
Why do you think string parsing has anything to do with AngularJS? Why don't you try using the substring function? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… –  JB Nizet 16 mins ago

1 Answer 1

In your controller you can do something as this:

function YourController($scope) {
   $scope.Action = "0876";

   // topmenu object for view
   $scope.Topmenu = { };

   // get last character in string and convert it to a number
   $scope.Topmenu.number = +$scope.Action.slice(-1);
}

Now the number 6 is available on your view as

{{ Topmenu.number }}

Note: if you try to convert an empty string to a number you'll get 0, if you don't want this behaviour you'll need to add a check before converting

share|improve this answer
    
Topmenu is something i already have in the view. It got some contents which can be accessed using Topmenu.index. Where index is a number. And that number should be this 6. You may edit your answer accordingly.Thanks for the help. Also what if I want the entire string after the 1st element or second element as number? can I still use slice? –  RBz just now

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.