2014-11-10 3 views
0

저는 목록에있는 각 국가에 대해 얼마나 많은 클릭이 있었는지와 전체 집계를 유지할 수있는 카운터를 설정해야합니다.각도 범위를 현재 범위로 제한

내가보기에는 지금까지 this fiddle에서 볼 수있는 아래 항목이 있습니다. 내가 겪고있는 문제는 각 국가마다 고유 한 카운트를 유지할 수 없다는 것입니다. 어떻게이 일을 성취 할 수 있습니까?

<div ng-app="myApp"> 
    <div data-ng-view></div> 
    </div> 

    'use strict'; 
    var myApp = angular.module('myApp', ['ngRoute', 'templates/view1.html', 'templates/view2.html']); 

    myApp.config(['$routeProvider', function ($routeProvider) { 
     $routeProvider 
      .when('/', { 
       templateUrl: 'templates/view1.html', 
       controller: 'CountryListCtrl' 
      }) 
      .when('/:id', { 
       templateUrl: 'templates/view2.html', 
       controller: 'CountryCtrl' 
      }) 
    }]); 

    myApp.factory('Countries', ['$q', function ($q) { 
     var countriesList = [];  
     // perform the ajax call (this is a mock) 
     var getCountriesList = function() { 
      // Mock return json 
      var contriesListMock = [ 
       { 
        "id": "0", 
         "name": "portugal", 
         "abbrev": "pt" 
       }, { 
        "id": "1", 
         "name": "spain", 
         "abbrev": "esp" 
       }, { 
        "id": "2", 
         "name": "angola", 
         "abbrev": "an" 
       } 
      ]; 
      var deferred = $q.defer(); 
      if (countriesList.length == 0) { 
       setTimeout(function() { 
        deferred.resolve(contriesListMock, 200, ''); 
        countriesList = contriesListMock; 
       }, 1000); 
      } else { 
       deferred.resolve(countriesList, 200, ''); 
      } 
      return deferred.promise; 
     } 

     var getCountry = function(id) { 
      var deferred = $q.defer(); 
      if (countriesList.length == 0) { 
       getCountriesList().then(
        function() { 
         deferred.resolve(countriesList[id], 200, ''); 
        }, 
        function() { 
         deferred.reject('failed to load countries', 400, ''); 
        } 
       ); 
      } else { 
       deferred.resolve(countriesList[id], 200, ''); 
      } 
      return deferred.promise; 
     } 

     var cnt  = 0; 
     var cntryCnt = 0;  

     var incCount = function() { 
       cnt++; 
       return cnt;   
     } 

     var incCntryCount = function(id) { 
       cntryCnt++; 
       return cntryCnt;   
     } 

     return { 
      getList: getCountriesList, 
      getCountry: getCountry, 
      getCount : function() { 
       return cnt; 
      }, 
      getCntryCount : function() { 
       return cntryCnt; 
      },   
      incCount: incCount, 
      incCntryCount: incCntryCount  
     }; 
    }]); 

    myApp.controller('CountryListCtrl', ['$scope', 'Countries', function ($scope, Countries) { 
     $scope.title = ''; 
     $scope.countries = []; 
     $scope.status = ''; 

     Countries.getList().then(
      function (data, status, headers) { //success 
       $scope.countries = data; 
      }, 
      function (data, status, headers) { //error 
       $scope.status = 'Unable to load data:'; 
      } 
     ); 
    }]); 

    myApp.controller('CountryCtrl', ['$scope', '$routeParams', 'Countries', function ($scope, $routeParams, Countries) { 
     $scope.country = { 
      id: '', 
      name: '', 
      abbrev: '' 
     }; 
     var id = $routeParams.id; 

     Countries.getCountry(id).then(
      function(data, status, hd) { 
       console.log(data); 
       $scope.country = data; 
       $scope.countOverall = Countries.getCount; 
       $scope.countCntry = Countries.getCntryCount; 
       $scope.clickCnt = function() { 
        $scope.countTotal = Countries.incCount(); 
        $scope.country.clicks = Countries.incCntryCount(id); 
        console.log($scope); 
       }; 
      }, 
      function(data, status, hd) { 
       console.log(data); 
      } 
     ); 

    }]); 


    angular.module('templates/view1.html', []).run(["$templateCache", function ($templateCache) { 
     var tpl = '<h1>{{ title }}</h1><ul><li ng-repeat="country in countries"><a href="#{{country.id}}">{{country.name}}</div></li></ul>'; 
     $templateCache.put('templates/view1.html', tpl); 
    }]); 

    angular.module('templates/view2.html', []).run(["$templateCache", function ($templateCache) { 
     var tpl = '<div>{{country.name}} clicks {{countCntry()}} <br> overall clicks {{countOverall()}}</div><button><a href="#">BACK</a></button><button ng-click="clickCnt()" >count clicks ++ </button>'; 
     $templateCache.put('templates/view2.html', tpl); 
    }]); 

답변

1

문제는 사용자가 국가에 따라 카운트를 증가시키지 않는다는 것입니다. 지금 당장 일하는 중.

편집 : 나는 바이올린 업데이트되었습니다

: http://jsfiddle.net/1xtc0zhu/2/

를 내가 기본적으로 cntryCnt 속성으로 국가 ID를 받아서 각 당 계산 권리를 유지 객체 리터럴을 만들고 있었다했다 아이디, 그래서 같은 '

var cnt  = 0; 
var cntryCnt = {}; 

... 

// The function now receives the country id and increments the specific country clicks only. 
var incCntryCount = function(id) { 
     cntryCnt[id] = cntryCnt[id] || 0; 
     cntryCnt[id]++; 
     return cntryCnt[id];   
} 

변경 나머지는 템플릿에 있고, 점점 또는 수를 증가시 기본적으로 만 PARAM으로 국가 ID를 보내고있다.

또한 각도 별 질문이 아니지만 일반적으로 더 많은 프로그래밍 문제입니다.

관련 문제