2017-05-16 5 views
0

私は私のhello worldプロジェクトに取り組んでいます。私は2つのページを "構成"と "構成の追加" * .htmlと呼んでいます。一人一人がこのような独自のコントローラがあります。今JavaScriptの継承/角

angular.module('MissionControlApp').controller('ConfigController', ConfigController); 

angular.module('MissionControlApp').controller('AddConfigController', AddConfigController); 

を、各コントローラは、いくつかの性質を持っている非常に多く、その重複:

AddConfigControlleraddFile()ので、私はちょうどコピーの同じ機能を持って望んでいる私の
function ConfigController($routeParams, ConfigFactory, $window){ 
    var vm = this; 
    vm.status; 
    vm.projectId = $routeParams.projectId; 
    vm.selectedProject; 
    vm.configurations; 
    vm.selectedConfig; 
    vm.selectedRecords; 
    vm.filteredConfig; 
    vm.newFile; 
    vm.fileWarningMsg = ''; 

vm.addFile = function(){ 
     var filePath = vm.newFile; 
     var encodedUri = encodeURIComponent(filePath); 
     vm.fileWarningMsg=''; 

     ConfigFactory 
      .getByEncodedUri(encodedUri).then(function(response){ 
       var configFound = response.data; 
       var configNames = ''; 
       var configMatched = false; 
       if(response.status === 200 && configFound.length > 0){ 
        //find an exact match from text search result 
        for(var i = 0; i < configFound.length; i++) { 
         var config = configFound[i]; 
         for(var j=0; j<config.files.length; j++){ 
          var file = config.files[j]; 
          if(file.centralPath.toLowerCase() === filePath.toLowerCase()){ 
           configMatched = true; 
           configNames += ' [' + config.name + '] '; 
           break; 
          } 
         } 
        } 
       } 
       if(configMatched){ 
        vm.fileWarningMsg = 'Warning! File already exists in other configurations.\n' + configNames; 
       } else if(filePath.length > 0 && filePath.includes('.rvt')){ 
        var file1 = { centralPath: filePath }; 
        vm.selectedConfig.files.push(file1); 
        vm.newFile = ''; 
       } else{ 
        vm.fileWarningMsg = 'Warning! Please enter a valid file.'; 
       } 

      }, function(error){ 
       vm.status = 'Unable to get configuration data: ' + error.message; 
      }); 

    }; 

同じコードを貼り付けましたが、C#から来て、私はここでいくつかのクラス継承を行うことができると確信して、ちょうどConfigControllerから継承して...右に拡張?

これはスーパーノブの質問です。それから謝罪する。 jsは私にとって謎のビットです。

function AddConfigController($routeParams, ConfigFactory, $window){ 
    var vm = this; 
    vm.status; 
    vm.projectId = $routeParams.projectId; 
    vm.selectedProject = {}; 
    vm.newConfig = {}; 
    vm.newFile; 
    vm.fileWarningMsg = ''; 

vm.addFile = function(){ 
     var filePath = vm.newFile; 
     var encodedUri = encodeURIComponent(filePath); 
     vm.fileWarningMsg=''; 

     ConfigFactory 
      .getByEncodedUri(encodedUri).then(function(response){ 
       var configFound = response.data; 
       var configNames = ''; 
       var configMatched = false; 
       if(response.status === 200 && configFound.length > 0){ 
        //find an exact match from text search result 
        for(var i = 0; i < configFound.length; i++) { 
         var config = configFound[i]; 
         for(var j=0; j<config.files.length; j++){ 
          var file = config.files[j]; 
          if(file.centralPath.toLowerCase() === filePath.toLowerCase()){ 
           configMatched = true; 
           configNames += ' [' + config.name + '] '; 
           break; 
          } 
         } 
        } 
       } 
       if(configMatched){ 
        vm.fileWarningMsg = 'Warning! File already exists in other configurations.\n' + configNames; 
       } else if(filePath.length > 0 && filePath.includes('.rvt')){ 
        var file1 = { centralPath: filePath }; 
        vm.selectedConfig.files.push(file1); 
        vm.newFile = ''; 
       } else{ 
        vm.fileWarningMsg = 'Warning! Please enter a valid file.'; 
       } 

      }, function(error){ 
       vm.status = 'Unable to get configuration data: ' + error.message; 
      }); 

    }; 

答えて

0

あなたが相続について尋ね、あなたはECMAScriptの5を使用しているように見えるので、私はObject.create()でご覧になることをお勧めしてみましょう。具体的には、classical inheritance exampleです。

AngularJSでは、ファイルや設定を管理し、addFile機能をそこに配置するのは、より良い解決策は、Serviceを作成することです。このようにして、両方のコントローラは、ファイルを追加するときに、サービスを挿入して同じ機能を呼び出すことができます。同様に、この機能にアクセスする必要のある他のサービスやコントローラーも同様に注入することができます。