当用户点击页面上的任何位置时,如何自动将文本区域清空。 Angularjs,Javascript?

时间:2017-02-08 12:22:57

标签: javascript angularjs angularjs-directive angularjs-scope

我在angularjs项目中工作,我有一个文本区域,它给我一些搜索结果。我想在我点击页面的任何地方时清除我的文本区域。我的代码是

private Camera mCamera=null;
private CameraPreview mPreview;
public FrameLayout preview;

        @Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera_preview);
    View myView= (View) findViewById(R.id.camera_previeww);
    myView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

    cameraID= Camera.CameraInfo.CAMERA_FACING_FRONT;
    mCamera=openCamera(cameraID);
    mCamera.startPreview();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) this.findViewById(R.id.camera_previeww);
    preview.addView(mPreview);
}

public void switchCamera(){

    mCamera.stopPreview();
    releaseCamera();
    if (cameraID==Camera.CameraInfo.CAMERA_FACING_BACK){
        cameraID=Camera.CameraInfo.CAMERA_FACING_FRONT;
    }else{
        cameraID=Camera.CameraInfo.CAMERA_FACING_BACK;
    }

    mCamera=openCamera(cameraID);
    mCamera.startPreview();

    CameraPreview mPreview = new CameraPreview(this, mCamera);
    preview = (FrameLayout) this.findViewById(R.id.camera_previeww);
    preview.removeAllViews();
    preview.addView(mPreview);

}
public  Camera openCamera(int cameraIDD){
    Camera c=null;
    try{
        c=Camera.open(cameraIDD);
    }catch (Exception e){
        Log.d("Camera Activity", e.getMessage());
    }
    return c;

}

4 个答案:

答案 0 :(得分:1)

如果您在项目中的某个地方使用JQuery,那么您可以使用它。



<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<script>
$(document).ready(function(){
$('div').click(function(){
debugger 
 
var check= $('#text').val();
if(check!=""){
$('#text').val('')

}
});
});
</script>
<body">

<div class="table-responsive container" style="background:cyan; height:100px">
<input type="text" id="text" ng-model="bigdata" ng-change="ctrl.getData(bigdata);searchDataC =true;" ng-model-options="{debounce: 1000}" placeholder="Search contacts" class="search-input ip-size-roster ng-pristine ng-valid ng-touched" aria-invalid="false" style="">

  </div>
  </body>
  </html>
&#13;
&#13;
&#13;

在任何地方点击DIV。

答案 1 :(得分:1)

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
<input type="text" ng-model="bigdata" ng-change="ctrl.getData(bigdata);searchDataC =true;" ng-model-options="{debounce: 1000}" placeholder="Search contacts" class="search-input ip-size-roster ng-pristine ng-valid ng-touched" aria-invalid="false" ng-blur="clear()">



</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.firstName = "John";

    $scope.clear = function(){
    $scope.bigdata="";
    }
});
</script>

</body>
</html>

w3school的工作链接 http://www.w3schools.com/code/tryit.asp?filename=FCK88MIMAKGW

答案 2 :(得分:0)

你应该使用ng-click =“reset1()”来执行点击事件。

// html

 <div ng-controller="MainCtrl" ng-click="reset1()">
    <textarea id="textarea" ng-model="txt">

    </textarea>

   <div>{{txt}}</div>

<!--<button id='btn' ng-click="txt=''">Reset textarea</button>-->
    <button id='btn'>Reset textarea</button>
</div>


//js

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

app.controller('MainCtrl', ['$scope', function ($scope) {
    $scope.txt='';

    $scope.reset1 = function() {
        $scope.txt = '';
    };
}]);

答案 3 :(得分:0)

在Html文件中

app.factory('clickAnywhereButHereService', function($document){
  var tracker = [];

  return function($scope, expr) {
    var i, t, len;
    for(i = 0, len = tracker.length; i < len; i++) {
      t = tracker[i];
      if(t.expr === expr && t.scope === $scope) {
        return t;
      }
    }
    var handler = function() {
      $scope.$apply(expr);
    };

    $document.on('click', handler);

    // IMPORTANT! Tear down this event handler when the scope is destroyed.
    $scope.$on('$destroy', function(){
      $document.off('click', handler);
    });

    t = { scope: $scope, expr: expr };
    tracker.push(t);
    return t;
  };
});
app.directive('clickAnywhereButHere', function($document, clickAnywhereButHereService){
  return {
    restrict: 'A',
    link: function(scope, elem, attr, ctrl) {
      var handler = function(e) {
        e.stopPropagation();
      };
      elem.on('click', handler);

      scope.$on('$destroy', function(){
        elem.off('click', handler);
      });

      clickAnywhereButHereService(scope, attr.clickAnywhereButHere);
    }
  };
});

在控制器文件中

$scope.clickedSomewhereElse = function () {
    //clear text here
};