【问题标题】:Issues clearing HTML input with angular ng-click使用角度 ng-click 清除 HTML 输入的问题
【发布时间】:2015-09-01 19:22:08
【问题描述】:

我想知道为什么我似乎无法使用 ng-click 清除文本框输入。 Here is a plunker example 我能够开始工作,但我在实际应用中遇到了不同的结果。在我的应用程序中,警报会触发,但实际文本并不清楚。关于什么可能会影响这一点的任何想法?传递其他依赖项($rootScopeplayerService)会导致我的应用出现任何问题吗?

如果有帮助,我可以提供更多代码。

.controller('HomeTabCtrl', function ($scope, $rootScope, playerService) {

$scope.yellText = "";
$scope.vote = playerService.voteForPlayer;

$scope.postToSlack = playerService.postToSlack;
$scope.postYellToSlack = playerService.postYellToSlack;
$scope.clearYellText = function() {
  $scope.yellText = "";
  alert("cleared");
}

})


<div class="list list-inset">
    <label class="item item-input">
        <input type="text" placeholder="Type Some Words!" ng-model="yellText" autocomplete="off">
    </label>
</div>
<button class="button button-full button-positive" ng-click="postYellToSlack(selectedPlayer, yellText); clearYellText();">
    Enter text to earn points
</button>

非常感谢您的宝贵时间。如果我不清楚或者您需要我提供任何其他信息,请告诉我。

【问题讨论】:

  • 附件fiddle的文本框中的数据被清除了吗?

标签: javascript angularjs angular-ngmodel html-input


【解决方案1】:

在 ngModel 上使用 $setViewValue,如此处所述https://docs.angularjs.org/api/ng/type/ngModel.NgModelController

所以,在你的情况下

$scope.yellText.$setViewValue('');

见:https://jsfiddle.net/totLccob/8/

【讨论】:

  • 收到undefined is not a function 错误。我会看一下文档和代码,看看我是否遗漏了什么。我所做的只是将您的代码复制并粘贴到函数中。
【解决方案2】:

原来我真正需要的是:

this.yellText = "";

代替:

$scope.yellText = "";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    相关资源
    最近更新 更多