如何检查如果ng-show为true,则在angularjs中执行某些操作

时间:2016-07-27 14:55:25

标签: angularjs

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

当有人按button标记时,会检查ng-show是否为true,然后会自动点击span

3 个答案:

答案 0 :(得分:1)

只需检查ngShow使用的条件:

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

控制器:

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

    }
}

答案 1 :(得分:1)

如果您希望在span元素上触发点击事件,则点击button后,当ng-show为真时:

DOM:

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

控制器:

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

编辑:虽然,以下方法更有意义:

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>

控制器:

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

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

答案 2 :(得分:0)

你可以写ng-click =&#34; someFunction&#34;在提交按钮,它将检查您的变量$ scope.mbrscemployee是否为真,然后您可以从那里调用后续函数。!