Ahmed
Ahmed

Reputation: 1776

How to check If ng-show is true then do something in angularjs

<div class ="container" ng-show="mbrscemployee=='yes'">
    <span class="btn">click<span>
</div>
<button class="submit">submit</button>

When someone press button tag, it will check if ng-show is true, then the span will be clicked automatically.

Upvotes: 0

Views: 1733

Answers (3)

κροκς
κροκς

Reputation: 590

If you want the click event to be triggered on the span element, after you click the button, when ng-show is true:

DOM:

<div class="container" ng-show="mbrscemployee=='yes'">
    <span id="myspan" class="btn">click<span>
</div>
<button ng-click="btnClick()" class="submit">submit</button>

Controller:

$scope.btnClick = function() {
    if ($scope.mbrscemployee == 'yes') {
        angular.element('#myspan').triggerHandler('click');
    }
}

Edit: Although, the following approach makes more sense:

DOM

<div class="container" ng-show="mbrscemployee=='yes'">
    <span ng-click="spanClick()" id="myspan" class="btn">click<span>
</div>
<button ng-click="btnClick()" class="submit">submit</button>

Controller:

$scope.spanClick = function() {
    // do something
}

$scope.btnClick = function() {
    if ($scope.mbrscemployee == 'yes') {
        $scope.spanClick();
    }
}

Upvotes: 1

Naveen
Naveen

Reputation: 830

you can write ng-click="someFunction" on submit button which will check your variable $scope.mbrscemployee is true and then you can call subsequent function from there.!

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

Just check the condition that the ngShow uses:

<div class ="container" ng-show="mbrscemployee=='yes'">
    <span class="btn">click<span>
</div>
<button ng-click="foo()" class="submit">submit</button>

And the controller:

$scope.foo = function() {
    if ($scope.mbrscemployee == 'yes') {

    }
}

Upvotes: 1

Related Questions