I have written hide()
and show()
functions in javascript and calling them on onmousedown
in HTML
code.
Functions as below:
function show() {
document.getElementById("formDiv").style.display = "block";
}
function hide() {
document.getElementById("formDiv").style.display = "none";
}
I want to convert this into Angularjs
code. Can I write like this?
$scope.show = function() {
document.getElementById("formDiv").style.display = "block";
}
$scope.hide = function() {
document.getElementById("formDiv").style.display = "none";
}
Can we use document.getElementById()
in Angularjs
? I am new to angularjs
. Can anyone please help me out for this?
You can use document. getElementById() in Angularjs, but manipulating HTML in a controller is not a good practice. I recommend you to create a directive and do this kind of stuff there. Save this answer.
Document.getElementById() The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly.
The getElementById() method returns an element with a specified value. The getElementById() method returns null if the element does not exist. The getElementById() method is one of the most common methods in the HTML DOM. It is used almost every time you want to read or edit an HTML element.
Accessing Form Elements using getElementById In order to access the form element, we can use the method getElementById() like this: var name_element = document. getElementById('txt_name'); The getElementById() call returns the input element object with ID 'txt_name' .
The beauty of Angular is that the state of your data should control what happens to your view.
In the case of the functions you are calling, it appears like you want to use maybe ng-show
/ng-hide
/ng-if
.
Angular Docs
<div class="elementYouWantToTarget" ng-show="expressionOrVariableThatEvaluatesToTrueOrFalseWhichWillEitherShowOrHideYourElementComingFromAnAngularControllerInThisElementsScope">
<stuff>...</stuff>
</div>
And in your controller
angular.controller("myAwesomeExampleControllerExplainingAngular", ["$scope", controllerFn]);
function controllerFn($scope) {
$scope.expressionOrVariableThatEvaluatesToTrueOrFalseWhichWillEitherShowOrHideYourElementComingFromAnAngularControllerInThisElementsScope = true;
}
ng-mousedown
directive and attach it to your element:<div class="elementYouWantToTarget"
ng-show="expressionOrVariableThatEvaluatesToTrueOrFalseWhichWillEitherShowOrHideYourElementComingFromAnAngularControllerInThisElementsScope"
ng-mousedown="expressionLivingInYourControllerSomewhereThatIsCalledWhenTheMouseDownDeventFiresOnYourElement()"
ng-mouseup="expressionDoingTheSameThingExceptForWhenTheMouseUpEventHappens()">
<stuff>...</stuff>
</div>
And in your controller (PS. I'm assuming on mouse up the element disappears, otherwise you don't need the ng-mouseup directive or functions bound to it).
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;
}
}
Even if you shouldn't have to, there's occasions where you want to do it (for instance, comunicating with other iframe). then, it's good to use $document for unit testing purposes. In that case: Inject $document in your controller and use it. Remember $document returns an array.
$document[0].getElementById('element-id')
For instance, you can reload another iframe's content with
$document[0].getElementById('element-id').contentDocument.location.reload(true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With