0
<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"이 표시됩니다. 나는