I have a main html page in which I have an anchor tag (looks like a button explained below), a drop down with some values and a check box. Code is given below.
- I have an anchor tag that makes it look like a button because it has a few CSS classes assigned to it. Classes are:
blue --> makes it into a blue btn inactive btn --> makes it an inactive btn
Classes are working fine.
I have a check box and
A Drop down with some values
The code for all the three are below:
Button Code (Actually an anchor)
<a id="submit-btn" class="inactive blue">
<span>Submit</span>
</a>
Check Box
<input ng-click="enableButton($event)" type="checkbox"/>
Drop down
<select id="drop-dwn">
<option> ....... some values
</select>
Aim: My objective is to remove the inactive class from the button if both the checkbox is checked and also a value is
selected from the dropdown. So currently what I am doing is I have a controller for this main html page which has the
function enableButton($event) associated to the checkbox. It contains the logic for adding and removing the CSS (currently I have not added the logic for taking care of the drop down) on the button. I do know that such DOM manipulations should not be done in the controller and hence wanted a solution on where to add this logic. I have read many tutorials but still cannot figure out the perfect way of doing it in angular. Do I need a customer directive or should it be done in a different way.
Controller Code:
allControllers.controller('SubmitBtnController',
['$scope', function($scope) {
$scope.enableButton = function($event) {
var checkbox = $event.target;
if (checkbox.checked) {
angular.element(document.querySelector("#submit-btn")).
removeClass("inactive").addClass("blue");
}
else if (!checkbox.checked) {
angular.element(document.querySelector("#submit-btn")).
removeClass("blue").addClass("inactive");
}
}
}]);
The Controller is tied to the main app.
Please help.