2016-08-10 5 views
1

저는 angularjs에 다소 익숙하며 서비스에 약간의 어려움을 겪고 있습니다. 그래서 나는 각도 응용 프로그램과 서비스로 할 일을하고 있었고 작동시키지 못했습니다. 이 내 컨트롤러앵귤러 JS 서비스가 작동하지 않습니다

.controller("myController", ['$scope','toDoService', function($scope,toDoService){ 

    var lists = toDoService.getLists(); 
    $scope.lists = lists; 

    $scope.add = function(){ 
     // WHAT TO PUT HERE???? 

    } 

이며,이

.service('toDoService', function(){ 
     this.getLists =function(){ 
      var list = [ 
       {"name" : "abebe" ,"done":false, id: Date.now()} 
      ]; 
      return list; 
     } 


     this.add = function(){ 
     $scope.lists.push({'name':$scope.nameSpace,'done':false, id: Date.now()}); 
     $scope.nameSpace = ''; 
     return this; 
     } 

    }); 

당신은이 같은 서비스 내에서 $ 범위를 사용할 수 없습니다 ADVANCE

+0

당신은 오류가 있습니까? – DMCISSOKHO

+0

nop, 내 콘솔에 오류가 없습니다. – Harun

답변

1

당신은 서비스 내에서 $ 범위를 사용할 수 없습니다

이 답변을 참조하십시오. 작동 방법을 이해하려면 Fiddle을 확인하십시오.

myApp.service('toDoService', function(){ 
      this.details = { 
     lists: [ 
       {"name" : "abebe" ,"done":false, id: Date.now()} 
      ] 
     } 
     this.getLists =function(){ 
      var list = [ 
       {"name" : "abebe" ,"done":false, id: Date.now()} 
      ]; 
      return list; 
     } 
     this.addList = function(item) { 
     this.details.lists.push(item) 
     } 
    }); 

myApp.controller("myController", ['$scope','toDoService',  
    function($scope,toDoService){ 
    $scope.lists = function() { return toDoService.details.lists }; 
    $scope.add = function(){ 
     // WHAT TO PUT HERE???? 
     var newListItem = {"name" : "abebe" ,"done":false, id: Date.now()} 
     toDoService.addList(newListItem) 
    } 
}]); 
0

다음은 전체 코드입니다.

.controller("myController", ['$scope','toDoService', function($scope,toDoService){ 

var lists = toDoService.getLists(); 
$scope.lists = lists; 

$scope.add = function(){ 
    toDoService.add($scope.lists); 

} 

서비스는 다음과 같습니다.

.service('toDoService', function(){ 

    this.lists = []; 

    this.getLists =function(){ 
     var list = [ 
      {"name" : "abebe" ,"done":false, id: Date.now()} 
     ]; 
     return list; 
    } 


    this.add = function(data){ 
     lists.push(data); 
     return lists; 
    } 

}); 
관련 문제