Angular Send HTTP requests after selecting checkboxes

Background: Im working on an angular app that displays a list of articles. The list can be changed by various settings. One such setting is the source of these articles. Think of the source as a news agency: the article comes from a specific source:

enter image description here

Therefore, when the user clicks the Sources link, a drop-down menu should appear that contains a list of sources. The user can select any combination of these sources. There are also buttons "Select All" and "Clear All" to select all sources or deselect all of them:

enter image description here

: , , HTTP- , .

, , , http- ( updateArticleList()).

1) ng-click label:

<ul>
  <li ng-repeat="source in sources">
    <label ng-click="updateArticleList()">
      <input type="checkbox" ng-model="source.selected">
      {{source.title}}
    </label>
  </li>
</ul>

( label , -, input). .

2) ng-change input:

<ul>
  <li ng-repeat="source in sources">
    <label>
      <input type="checkbox" ng-model="source.selected"
       ng-change="updateArticleList()">
      {{source.title}}
    </label>
  </li>
</ul>

, " " "", HTTP-. .

, setTimeout, , ( ng-click):

    var requestAllowed = true;

    var debounceRequests = function(){
      requestAllowed = false;
      setTimeout(function(){
        requestAllowed = true; 
      }, 5);
    };

    scope.updateArticleList = function(){
      if (requestAllowed === true){
        // prevent the second call to the function from ng-click
        debounceRequests();
        // also, give time for the input to register ng-click on the label
        setTimeout(function(){
             // finally, send an http request
             getArticles();
        }, 5);
      }
    };

.

, , HTTP- ?

, js-.

==================

:

, " " :

    scope.selectAllSources = function(){
      scope.sources.forEach(function(source){
        source.selected = true;
      });
      scope.updateArticleList();
    };
+4
3

ng-change.

ng-change , , . selectAll . , " " -.

$scope.selectAll = function(){
  for (var i = 0; i < $scope.sources.length; i++) {
    $scope.sources[i].selected = true; // this does not fire `ng-change`
  }

  $scope.updateArticleList();
}

: OP , , :

<li ng-repeat="source in sources">
   <label>
      <input type="checkbox" ng-model="source.selected" ng-change="updateArticleList()">
      {{source.title}}
   </label>
</li>
<button ng-click="selectAll()">select all</button>
+2

, : Angular.js ng-click

, , , , .

    $scope.updateArticleList = function(event){
        if(event.toElement.tagName == 'LABEL'){
            //run code
        }
    };

HTML

<label ng-click="updateArticleList($event)">

JSFiddle: http://jsfiddle.net/4p48q63j/

+1
<ul>
  <li ng-repeat="source in sources">
    <label>
      <input type="checkbox" ng-model="source.selected"
       ng-change="updateArticleList(source.selected)">
      {{source.title}}
    </label>
  </li>
</ul>

; ....

$scope.updateArticleList=function(checked)
{
    if(checked==true)
    {
      //Call to service
    }
}

selecteAll (/) ....

    $scope.selectAll = function(){
    angular.forEach($scope.sources,function(source,$index){
       // model value will be true for all sources....
       //Call to service
    });
    };
0

All Articles