0

I have this variable:

var number = "1,10,25,60";

How can I parse it inside a view into this form?

<tr>
   <td ng-click="ctrl.set(1)>1</td>
   <td ng-click="ctrl.set(10)>10</td>
   <td ng-click="ctrl.set(25)>25</td>
   <td ng-click="ctrl.set(60)>60</td>
</tr>

In normal js I would just do var numbers = number.split(","); and then some loop like so:

var html;
for (i = 0; i <= numbers.length; i++) {
   html += "<td ng-click='ctrl.set" + numbers[i] + "'>" + numbers[i] + "</td>";
}

Maybe some directive?

2 Answers 2

2

Assign it to a $scope var (the split result)

var number = "1,10,25,60";
$scope.numbers = number.split(",");

And the view:

<tr ng-repeat="num in numbers">
    <td ng-click="ctrl.set(num)>{{num}}</td>
</tr>
Sign up to request clarification or add additional context in comments.

4 Comments

well but I don't have that variable inside js file, it's inside view
What do you mean, it's in the view?
well, I got an array of items and each has its own numbers. So I just can't do that like your example, but I will try to figure out something
If you could post what you actually have, it'll help.
0

ng-repeat will iterate through an array:

<html>
  <head>
    <script data-require="[email protected]" src="https://code.angularjs.org/1.3.17/angular.js"></script>
   </head>
<body  ng-app  >
    <input ng-model="$scope.numbers" ng-init="$scope.numbers = [1,10,25,60]"/>
    <table>
      <tbody>
        <tr>
          <td ng-repeat="n in $scope.numbers track by $index"
              ng-click="console.log(n);$scope.selected = n">{{n}}
          </td>
        </tr>
      </tbody>
    </table>
    <div>{{$scope.selected}}</div>
  </body>
</html>

2 Comments

Dont use $scope in the view files!
Don't use $scope at all, use controllerAs! But this demo showing ng-repeat is all defined in the HTML.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.