[英]How to use document.getElementById() method in TypeScript?
[英]How to use document.getElementById() method in Angularjs
我在javascript中編寫了hide()
和show()
函數,並在HTML
代碼中調用onmousedown
。
功能如下:
function show() {
document.getElementById("formDiv").style.display = "block";
}
function hide() {
document.getElementById("formDiv").style.display = "none";
}
我想將其轉換為Angularjs
代碼。 我可以這樣寫嗎?
$scope.show = function() {
document.getElementById("formDiv").style.display = "block";
}
$scope.hide = function() {
document.getElementById("formDiv").style.display = "none";
}
我們可以在Angularjs
使用document.getElementById()
嗎? 我是angularjs
。 有人可以幫幫我嗎?
Angular的優點在於您的數據狀態應該控制您的視圖會發生什么。
對於您正在調用的函數,看起來您可能想要使用ng-show
/ ng-hide
/ ng-if
。
<div class="elementYouWantToTarget" ng-show="expressionOrVariableThatEvaluatesToTrueOrFalseWhichWillEitherShowOrHideYourElementComingFromAnAngularControllerInThisElementsScope">
<stuff>...</stuff>
</div>
並在你的控制器
angular.controller("myAwesomeExampleControllerExplainingAngular", ["$scope", controllerFn]);
function controllerFn($scope) {
$scope.expressionOrVariableThatEvaluatesToTrueOrFalseWhichWillEitherShowOrHideYourElementComingFromAnAngularControllerInThisElementsScope = true;
}
ng-mousedown
指令並將其附加到您的元素: <div class="elementYouWantToTarget"
ng-show="expressionOrVariableThatEvaluatesToTrueOrFalseWhichWillEitherShowOrHideYourElementComingFromAnAngularControllerInThisElementsScope"
ng-mousedown="expressionLivingInYourControllerSomewhereThatIsCalledWhenTheMouseDownDeventFiresOnYourElement()"
ng-mouseup="expressionDoingTheSameThingExceptForWhenTheMouseUpEventHappens()">
<stuff>...</stuff>
</div>
在你的控制器中(PS。我假設在鼠標上移動元素消失,否則你不需要ng-mouseup指令或綁定到它的函數)。
angular.controller("myAwesomeExampleControllerExplainingAngular", ["$scope", controllerFn]);
function controllerFn($scope) {
$scope.expressionOrVariableThatEvaluatesToTrueOrFalseWhichWillEitherShowOrHideYourElementComingFromAnAngularControllerInThisElementsScope = false;
$scope.expressionLivingInYourControllerSomewhereThatIsCalledWhenTheMouseDownDeventFiresOnYourElement = function reallyLongButShouldBeNamedStillFn($event) {
//setting variable to true shows element based on ng-show
$scope.expressionOrVariableThatEvaluatesToTrueOrFalseWhichWillEitherShowOrHideYourElementComingFromAnAngularControllerInThisElementsScope = true;
}
$scope.expressionDoingTheSameThingExceptForWhenTheMouseUpEventHappens() = nameAllYourFunctionsFn($event) {
//setting variable to false hides element based on ng-show.
$scope.expressionOrVariableThatEvaluatesToTrueOrFalseWhichWillEitherShowOrHideYourElementComingFromAnAngularControllerInThisElementsScope = false;
}
}
即使你不應該這樣做,也有時候你想要這樣做(例如,與其他iframe交流)。 那么,使用$ document進行單元測試是件好事。 在這種情況下:在控制器中注入$ document並使用它。 記住$ document返回一個數組。
$document[0].getElementById('element-id')
例如,您可以重新加載另一個iframe的內容
$document[0].getElementById('element-id').contentDocument.location.reload(true);
您可以在Angularjs中使用document.getElementById()
,但在控制器中操作HTML不是一個好習慣。
我建議你創建一個指令並在那里做這種事情。
您可以使用ngShow
來控制樣式,使用ngMousedown
來設置標志:
<form id="formDiv" ng-show="isVisible" ng-mousedown="isVisible=false" ng-mouseup="isVisible=true"></form>
在你的控制器中,只需設置$scope.isVisible = true;
(或false
)在州之間切換。 您還可以使用ngHide
來控制是否根據表達式隱藏元素。
ngShow
和ngHide
作用是切換設置display:none;
的ng-hide
類display:none;
對元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.