0
<body ng-app="EditApp">
<div ng-controller="MainController">
<h1 click-to-edit>This is something I want to edit</h1>
</div>
</body>
的Javascript:
angular.module("EditApp", [])
.controller("MainController", ['$scope', function($scope){
$scope.text = "This is something I would like to edit";
}])
.directive("clickToEdit", function(){
return {
restrict: 'EA',
template: " <input type='text' value='{{ text }}' ng-show='showInput'/> <div ng-transclude ng-hide='showInput'></div>",
transclude: true,
link: function (scope, element, attrs) {
scope.showInput = false
element.on("click", function(){
scope.$parent.showInput = true
})
}
}
})
爲什麼showInput
不改變?或者是,但我必須做一個scope.$apply()
?我是否應該在點擊功能中以某種方式傳遞scope
?
謝謝!
是的!這是完美的 - 不能相信我沒有想到ngclicks – 2014-09-22 20:22:32
@MohamedElMahallawy儘可能多我會建議採用角度註冊事件的方式,所以你最終不會遇到這樣的範圍同步問題。:) – PSL 2014-09-22 20:24:56