2016-12-20 10 views
1

私はAngularJSを初めて使用しており、指示の概念と混同しています。 $ scope.delete関数を介して2番目のディレクティブのscope.markersを更新していますが、2方向データバインディング分離スコープを使用しているため、変更に最初のディレクティブが反映されていません。どのような解決策が大きな助けになるでしょう。スコープ通信をディレクティブからディレクティブに分離しますか?

私はのように私の最初のディレクティブがあります。次のようにダメージ図ディレクティブのための私のhtmlコードがある

app.directive('damageMarker', function ($mdDialog,inspectionService,$timeout) { 
    return { 
     restrict: 'AE', 
     require: '?^damageDiagram', 
     scope: { 
      marker: '=', 
      pointsrc: '=', 
      dtype:'=', 
      markers: '=' 
     }, 
     template: '<img ng-src="{{pointsrc}}" />', 
     link: function (scope, elem, attr) { 
      elem.on("click",function(e){     
       showDialog(); 
       function showDialog($event) { 
        var parentEl = angular.element(document.body); 
        $mdDialog.show ({ 
        parent: parentEl, 
        targetEvent: $event, 
        template: 
         '<form name="clientForm" novalidate >'+ 
         '<md-dialog aria-label="List dialog">' + 
         ' <md-dialog-content>'+       
          '<md-input-container>'+ 
           '<md-select ng-model="dtypeValue" class="dialog-close" placeholder="Select dtype">'+ 
           '<md-option ng-repeat="opt in dtype">{{opt}}</md-option>'+ 
           '</md-select>'+ 
          '</md-input-container>'+ 
          '<md-input-container class="md-block">'+ 
           '<label>Comment</label>'+ 
           '<input required name="name" ng-model="comment" class="dialog-close">'+ 
           '<div ng-messages="clientForm.name.$error">'+ 
            '<div ng-message="required">This is required.</div>'+          
           '</div>'+ 
          '</md-input-container>'+ 
         ' </md-dialog-content>' + 
         ' <div class="md-actions" layout="row" layout-align="end center">' + 
         ' <md-button ng-click="closeDialog()" class="md-primary">' + 
         '  Close' + 
         ' </md-button>' + 
          '<md-button ng-disabled="clientForm.$invalid" ng-click = "save()" class="md-primary">'+ 
           'Save'+ 
          '</md-button>'+ 
          '<md-button ng-disabled="clientForm.$invalid" ng-click = "delete()" class="md-primary">'+ 
           'Delete'+ 
          '</md-button>'+ 
         ' </div>' + 
         '</md-dialog>'+ 
         '</form>',      
        controller: DialogController 
        }); 
        function DialogController($scope, $mdDialog) { 
        $scope.dtypeValue = scope.dtype[scope.marker.dtype.toUpperCase()]; 
        $scope.dtype = scope.dtype; 
        $scope.comment = scope.marker.comment; 
        $scope.marker = scope.marker; 
        $scope.closeDialog = function() { 
         $mdDialog.hide(); 
        } 
        $scope.save = function(){       
         console.log($scope.marker.id); 
         console.log($scope.dtypeValue); 
         console.log($scope.comment); 
         var dataSend = {}; 
         dataSend.id = $scope.marker.id; 
         dataSend.comment = $scope.comment; 
         for(var key in $scope.dtype) { 
          if($scope.dtype[key] == $scope.dtypeValue) { 
           dataSend.dtype = key; 
          } 
         } 
         inspectionService.updateDiagram(dataSend).then(function(response){ 
          console.debug("response ; "+response); 
          $mdDialog.hide();        
          scope.marker.id = response.id; 
          scope.marker.comment = response.comment; 
          scope.marker.dtype = response.dtype; 
         }); 
        } 
        $scope.delete = function(){ 
         var dataSend = {}; 
         dataSend.id = $scope.marker.id;  
         var param = {}; 
         param.inspection=$scope.marker.inspection;           
         inspectionService.deleteDiagramMarker(dataSend).then(function(response){                      
          inspectionService.getDiagram(param).then(function(response){        
           $timeout(function() { 
            scope.$apply(function(){ 
             scope.markers = response.results; 
            })         
           },2000);                 
           console.debug("response ; "+response); 
           $mdDialog.hide(); 
          });  
         }); 
        } 

        } 
       } 
      }); 
      console.log(scope.marker.top, scope.marker.left, elem); 
     } 
    } 
}); 

:として

app.directive('damageDiagram', function() { 
    return { 
    retrict: 'AE', 
    scope: { 
     imgsrc: '=', markers: '=', pointsrc: '=', dtype:'=' 
    }, 
    templateUrl: 'app/components/inspections/damage-diagram.html', 
     link: function (scope, element, attrs) {    
    } 
    } 
}); 

と第二のディレクティブを

<damage-diagram imgsrc="imgsrc" pointsrc="pointsrc" dtype="dtype" 
    markers="inspection.damage_diagram"> 
</damage-diagram> 

と私のhtmlコードは以下の通りです:

次のように
<div style="position:relative"> 
    <img id="23467129" ng-src="{{imgsrc}}" style="position:relative" width="100%" /> 
    <div ng-repeat="marker in markers" 
     marker="marker" 
     markers="markers" 
     dtype="dtype" 
     pointsrc="pointsrc" 
     damage-marker> 
    </div>  
</div> 

コントローラは:

app.controller('InspectionDetailCtrl', ['$scope', 'inspectionService', '$stateParams', 'Restangular','$rootScope', 
    function ($scope, inspectionService, $stateParams, Restangular, $rootScope) { 
     $scope.updateDamageImage = {}; 
     $scope.insp_id = $stateParams.inspId; 
     $scope.damageImagesShow = false; 
     $scope.comments = []; 
     $scope.types = []; 
     $scope.selectTypeDelete = false; 
     $scope.commentDelete = false; 
     $scope.selectTypeDeleteBefore = true; 
      $scope.commentDeleteBefore = true; 

     init($scope.insp_id); 
     console.log("Fetching details for scope", $scope.insp_id); 
     function init(insp_id) 
     { 
      inspectionService.inspections.customGET(insp_id, {type: 'full'}) 
      .then(function (data) { 
       $scope.inspection = data; 
      }, function (err) { 
       $scope.inspection = null; 
      }); 
     }   
     $scope.pointsrc="app/components/inspections/pointer.png"; 
     $scope.dtype = { 
      'S': 'Scratch (minor)', 
      'DS': 'Deep Scratch', 
      'D': 'Dents', 
      'WD': 'Wheel Damage', 
      'CW': 'Cracked Window', 
      'FT': 'Flat Tire', 
      'BL': 'Broken (lights)' 
     }; 
    }]); 
+0

いい仕事です...しかし、あなたが関連するctrl.jsを共有すると、それが実現します –

+0

ctrl.jsは共有されています –

答えて

0

指令に結合全て双方向の最初の変更がメインコントローラに反映されるような作品は指令ではなく、他の方法で指令の変化を見ることができませんメインコントローラには反映されません。

しかし、あなたがオブジェクトのインスタンスがそのように作成された一度だけ任意のディレクティブの変更はどこにでも反映されますので、その後、あなたはディレクティブで同じ変数を使用し、メインコントローラに

var x={}; 
x.value='to be passed in directive' 

を、オブジェクトを作成することができます解決策があります。

関連する問題