文本框未被清除

时间:2014-08-17 13:52:53

标签: angularjs angularjs-directive angularjs-scope

我正在使用Angular。

在我的控制器中我有:

mainApp.controller('loginBoxController', ['$scope', '$state', 'mainWebService', function($scope, $state, mainWebService) {

    $scope.usernameText = "";
    $scope.clear_fields = function() {
        $scope.usernameText = "";
    },

    $scope.btnCancel = { 
        width:'45%', 
        height:'30px', 
        roundedCorners: 'all',
        click : function(){
            $scope.clear_fields();
        }
    };
}]);

在我的HTML中,我有

<div >
    <input ng-jqxinput="txtusername" ng-model="usernameText" type="text" id="txtusernameID"/>
</div>

那么,我哪里出错了。请帮帮我。

2 个答案:

答案 0 :(得分:0)

请尝试此操作,创建新的单页应用并运行它。它会解决你的问题。

<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.18/angular.js"></script>
    <script>
        var mainApp = angular.module('MyApp', []);

            mainApp.controller('loginBoxController', ['$scope', function($scope) {
                $scope.usernameText = "";
                $scope.clear_fields = function() {
                $scope.usernameText = "";
            },

            $scope.btnCancel = { 
                width:'45%', 
                height:'30px', 
                roundedCorners: 'all',
                click : function(){
                    $scope.clear_fields();
                }
            };
        }]);
    </script>
  </head>

  <body ng-app="MyApp" ng-controller="loginBoxController">
    <div >
      <input ng-jqxinput="txtusername" ng-model="usernameText" type="text" id="txtusernameID"/>
      <input type ="button" value="submit" ng-click="clear_fields()"/>
    </div>
  </body>

</html>

答案 1 :(得分:0)

 $scope.$apply() after the $scope.clear_fields(); 

解决了我的问题。

 $scope.btnCancel = { 
    width:'45%', 
    height:'30px', 
    roundedCorners: 'all',
    click : function(){
        $scope.clear_fields();
        $scope.$apply();
    }
};