Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角JS服务不工作_Javascript_Angularjs - Fatal编程技术网

Javascript 角JS服务不工作

Javascript 角JS服务不工作,javascript,angularjs,Javascript,Angularjs,所以我对angularjs有点陌生,在服务方面遇到了一些困难。因此,我正在开发angularjs TO DO应用程序和服务,但无法使其正常工作。 这是我的控制器 .controller("myController", ['$scope','toDoService', function($scope,toDoService){ var lists = toDoService.getLists(); $scope.lists = lists; $scope.add = f

所以我对angularjs有点陌生,在服务方面遇到了一些困难。因此,我正在开发angularjs TO DO应用程序和服务,但无法使其正常工作。 这是我的控制器

.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;
      }

  });

提前感谢您

您不能在这样的服务中使用$scope


请参阅此答案:

您不能在服务内部使用$scope。检查以了解它应该如何工作

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)
    }
}]);

下面是完整的代码

.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;
    }

});

你有什么错误吗?没有,我的控制台没有错误