Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.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
Angularjs 可重用和可测试代码的代码结构_Angularjs - Fatal编程技术网

Angularjs 可重用和可测试代码的代码结构

Angularjs 可重用和可测试代码的代码结构,angularjs,Angularjs,我有一个Angular JS应用程序,它正在与REST API通信。该应用程序有一个管理部分,允许用户创建、读取、更新和删除资源 有几个不同的资源,每个资源一部分,我将每个成员定义为某种类型的输入字段。我已经创建了一个指令,它为模板的公共内容创建了一个双向绑定 我还为我的每个资源创建了一个ngResource 现在来谈谈我的问题。我有一个用于一个资源工作的控制器,它主要包含直接从部分调用的函数(例如submit()),并与ngResource通信 除了少数例外,代码本身可以复制到所有其他控制器,

我有一个Angular JS应用程序,它正在与REST API通信。该应用程序有一个管理部分,允许用户创建、读取、更新和删除资源

有几个不同的资源,每个资源一部分,我将每个成员定义为某种类型的输入字段。我已经创建了一个指令,它为模板的公共内容创建了一个双向绑定

我还为我的每个资源创建了一个ngResource

现在来谈谈我的问题。我有一个用于一个资源工作的控制器,它主要包含直接从部分调用的函数(例如submit()),并与ngResource通信

除了少数例外,代码本身可以复制到所有其他控制器,只需更改资源名称。这就是我想要避免的

如何创建一个抽象的ParentCtrl并以某种方式注入当时所需的资源? 这是可测试的吗?这是解决问题的坏方法吗

通常我会尝试抽象一个模型,而不是一个控制器,但我真的看不出在这种情况下是如何抽象的

编辑:添加代码

资源:

angular.module('experienceServices', ['ngResource'])
  .factory('Experience', function($resource){
    return $resource('/api/experience/:id',
      {'id': '@_id'}, {edit: { method: 'PUT' }});
  });
控制器:

App.controller('ExperienceCtrl', function( $scope, Experience ) {
  $scope.resources = [];
  $scope.currentItem = {};

  $scope.updateResources = function(){
    $scope.resources = Experience.query();
  };

  $scope.findById = function(id){
    for (var i = 0; i < $scope.resources.length; i++){
      if ($scope.resources[i]._id === id){
        return $scope.resources[i];
      }
    }
  };

  $scope.showItem = function(id){
    $scope.currentItem = findById(id);
  };

  $scope.create = function (experience){
    Experience.save(angular.copy(experience), function(){
      $scope.updateResources();
    });
  };

  $scope.editItem = function (experience){
    var item = angular.copy(experience);
    if (!item.hasOwnProperty('_id')){
      alert('Can not modify non existing item');
    }
    Experience.edit(item, function(res){
      $scope.updateResources();
    });
  };

  $scope.edit = function(id){
    $scope.experience = $scope.findById(id);
  };

  $scope.del = function(id){
    Experience.remove({ id: id }, function(){
      $scope.updateResources();
    });
  };
  $scope.updateResources();
});
App.controller('ExperienceCtrl',函数($scope,Experience){
$scope.resources=[];
$scope.currentItem={};
$scope.updateResources=函数(){
$scope.resources=Experience.query();
};
$scope.findById=函数(id){
对于(变量i=0;i<$scope.resources.length;i++){
if($scope.resources[i]。\u id==id){
返回$scope.resources[i];
}
}
};
$scope.showItem=函数(id){
$scope.currentItem=findById(id);
};
$scope.create=功能(体验){
Experience.save(angular.copy(Experience),function(){
$scope.updateResources();
});
};
$scope.editItem=功能(体验){
var项目=角度。复制(经验);
如果(!item.hasOwnProperty(“\u id”)){
警报(“无法修改不存在的项”);
}
经验。编辑(项目、功能(res){
$scope.updateResources();
});
};
$scope.edit=函数(id){
$scope.experience=$scope.findById(id);
};
$scope.del=函数(id){
Experience.remove({id:id},function()){
$scope.updateResources();
});
};
$scope.updateResources();
});
指令:

App.directive('resources', function (){
    return {
      scope: {
        list: '=',
        display: '@',
        edit: '&',
        del: '&'
      },
      template: '<div ng-repeat="elem in list">Name: {{ elem[display] }}' +
                ' <button ng-click="edit({ item: elem._id })">Edit</button> ' +
                ' <button ng-click="del({ item: elem._id })">Delete</button> ' +
                '</div>'
    };
  });
App.directive('resources',function(){
返回{
范围:{
列表:“=”,
显示:“@”,
编辑:“&”,
德尔:“&”
},
模板:“名称:{{elem[display]}”+
“编辑”+
“删除”+
''
};
});

如何将整个API包装到服务中,并从控制器访问它?您可以将资源类型作为参数传递给函数

您可以发布一些代码吗?这类问题最好是具体回答,而不是抽象回答。你完全正确。一个服务实际上就是“模型”,如果我把我所有的方法都放在一个服务中,把它导出为一个对象,然后把类型传递给服务。这样,服务是可重用的,而不是控制器。谢谢你给了我解决这个问题的动力。