1

In my code I have several markup items like so:

<div class="sli1"
 data-values="10, 20, 30, 40,50, 60, 70"

<div class="sli2"
 data-values="20, 40,60, 80,100, 120, 500"

What I have at the moment is a very simple:

sli1 =  $('#sli1').attr('data-values')

How can I improve this with Angular models, so I can access the model via either the controller or view?

Edit for abit more code:

In my view where I have:

<div class="sli1"
 data-values="... 

The values are embedded via the server right into the view, rather than the controller.

In the controller I declare the models on the JS vars:

$scope.new['test1'] = sli1;
$scope.new['test2'] = sli2;

That I can then declare certain points like:

$scope.new['test1'][0]
$scope.new['test1'][1];
$scope.new['test1'][2];
$scope.new['test1'][3];
$scope.new['test1'][4];

To get the array index values, just wondering if I can do it better with angular directly rather than rely on the JS vars.

1 Answer 1

1

You can set the values as ng-model and then access them from controller via the $scope:

<div class="sli1" ng-model="arrayWithValues1"
<div class="sli2" ng-model="arrayWithValues2"

.controller('SomeCtrl', function() {

  var array1 = $scope.arrayWithValues1;
  var array2 = $scope.arrayWithValues2;
});

Also, don't do DOM manipulation in controllers, if that's what you've been doing.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't have access to the values to embed into the controller, as they come server-side, otherwise that would be as what you suggested :) Rather I pull them across client side into the view and can use them there.
@Poiro Could you share a bit more code then? So I can update my answer. Also how do you add them to your view from the backend?
@Poiro Based on the use case that you've provided then my solution is what you want, you should never use jQuery in controllers, that belongs in directives and directives only.
Thanks, I'll review it now and see how I get on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.