ng-click =“check = true”和ng-click =“alterCheck()”之间的区别是什么?

时间:2016-07-13 01:16:43

标签: angularjs

我在项目中使用Angular,但遇到了问题 当我使用ng-click="check=true"时,在提交表单后,div仍会显示,而不是按照我的预期隐藏;使用ng-click="alterCheck()"或将check=true添加到ng-submit表达式可以解决此问题。那么这个问题的潜在原因是什么?

<div ng-show="check && form.$invalid"></div>
<!-- <form name="form" ng-submit="check=true && init()"> -->
<form name="form" ng-submit="init()">
    <input type="text" ng-model="text" required/>
    <!--     <input type="submit" ng-click="alterCheck()"/> -->
    <input type="submit" ng-click="check=true"/>
</form>

1 个答案:

答案 0 :(得分:0)

隐藏和解决条件

<强> HTML

<form name="form" >
          <input type="text" ng-model="text" required />
          <input type="submit"  ng-click="do_something()" ng-disabled="form.$invalid"/>
      </form>
   <div ng-hide="!button_clicked && !form.$invalid" >  Invalid Div  </div>

JS

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
   $scope.button_clicked = true;
    $scope.do_something = function() {
        alert("Clicked!");
        $scope.button_clicked = false;

    }
});

供参考https://plnkr.co/edit/SIi5hVY3v4NO5MRoNwEL?p=preview