2014-09-25 3 views
0

나는AngularJS와 지침 업데이트 부모

<div ng-controller="productDetailController" class="product-title"> 
    <div click-to-edit="product.name"></div> 
</div> 

처럼 사용되는

HTML에서
priceApp.directive("clickToEdit", function() { 
    var editorTemplate = '<div class="click-to-edit">' + 
     '<div ng-hide="view.editorEnabled">' + 
      '{{value}} ' + 
      '<a ng-click="enableEditor()">Edit</a>' + 
     '</div>' + 
     '<div ng-show="view.editorEnabled">' + 
      '<input ng-model="view.editableValue">' + 
      '<a ng-click="saveValue()">Save</a>' + 
      ' or ' + 
      '<a ng-click="disableEditor()">cancel</a>.' + 
     '</div>' + 
    '</div>'; 

    return { 
     restrict: "A", 
     replace: true, 
     template: editorTemplate, 
     transclude: true, 
     scope: { 
      value: "=clickToEdit", 
     }, 
     link: function($scope,$element,$attrs) { 
      $scope.view = { 
       editableValue: $scope.value, 
       editorEnabled: false 
      }; 

      $scope.enableEditor = function() { 
       $scope.view.editorEnabled = true; 
       $scope.view.editableValue = $scope.value; 
      }; 

      $scope.disableEditor = function() { 
       $scope.view.editorEnabled = false; 
      }; 

      $scope.saveValue = function() { 
       $scope.value = $scope.view.editableValue; 
       $scope.disableEditor(); 
       $scope.$emit('update'); 
      }; 
     } 
    }; 
}); 

절연 범위를 사용하여이 지침을 가지고 그리고 내 컨트롤러가 있습니다

$scope.$on('update',function(){ 
    saveProduct($scope.product); 
}); 

매우 흥미로운 것은 $ scope. $ on의 값이 반복되는 것입니다. 필드에 값 "test 1"을 넣고 지시문에서 save 메소드를 호출하면 이전 값 (초기 값)을 얻습니다.이 후 "테스트 2"를 입력하면 컨트롤러에 "텍스트 1"이 표시됩니다. 나는

답변

0

이 솔루션은 $ 제한 시간을 사용하여 해킹입니다 ... 잘못 어디 알고 싶어

$scope.$on('update',function(){ 
    $timeout(function(){ 
     saveProduct($scope.product); 
    }, 0);  
});