1

I want to bind/unbind the keypress when user clicks to the checkbox. I tried to code a bit but not work at all without error in javascript console.

HTML

<textarea name="message" ng-model="message" ui-keypress="btnEnter"></textarea>
<input type="checkbox" ng-click="bindKeyEnter($event)">

JS

function MyCtrl($scope) {
     $scope.btnEnter = {};

     $scope.bindKeyEnter = function(e) {
         var checkbox = e.target;
         $scope.btnEnter = checkbox.checked ? {enter: 'sendMessage()'} : {}
     };

     $scope.sendMessage = function() { console.log($scope.message); }
}

Your suggestion?

1 Answer 1

3

You could do it like this:

Html:

<div ng-controller="TstCtrl">
    <input ui-keypress="{enter: enter}" />
    <input type="checkbox" ng-model="doOnEnter" />
</div>

Javascript:

app.controller('TstCtrl', function ($scope) {
    $scope.enter = function () {
        if ($scope.doOnEnter)
            realOnEnter();
    };

    function realOnEnter() {
        console.log('On enter');
    }
});

See working jsfiddle here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.