0

How can I implement the following function in Angularjs?

var TEST = [];
$('#div').find('select.test').each(function(){TEST.push($(this).val())});

And then with this values I would like update the ng-model value, like this:

$scope.formData.TEST = TEST.join(",");
wpdaniel
  • 714
  • 8
  • 25
  • don't try to use JQuery with Angular like this. Angular already has two way binding with `ng-model`, it is unnecessary to use `.val()`. Read a bit more about how Angular works, and avoid interacting with the DOM. see https://stackoverflow.com/q/14994391/2495283 for more on the many differences in the ways these frameworks think. – Claies Oct 18 '17 at 09:49
  • in this specific case, you should define `TEST` as an array of objects, rather than just an empty array, and then use `ng-repeat` to iterate through the test array, to bind each object to an element in the HTML – Claies Oct 18 '17 at 09:54
  • @Claies i dont want use jquery in angular. I would like implement this function in angular way... – wpdaniel Oct 18 '17 at 10:36
  • that function doesn't make sense in angular, because in angular you don't retrieve the values from the DOM the way that function does. – Claies Oct 18 '17 at 10:37

1 Answers1

0

This is my solution:

    var TEST_ITEMS = [];
    var testValues = document.getElementsByClassName('test');
    for (var i = 0; i < testValues.length; ++i) {
        var item = testValues[i];
        var _testValues = item.options[item.selectedIndex].value;
        console.log(_testValues);
        TEST_ITEMS.push(_testValues);
    }
    $scope.formData.TEST = TEST_ITEMS.join(",");
wpdaniel
  • 714
  • 8
  • 25