1

This is a problem that can be easily done by declaring a scope function but I was wondering if there is a better way to make this easier

The code is:

<!DOCTYPE html>
<html ng-app="myApp">

  <head>
    <link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
  </head>

  <body ng-controller="myCtrl">
    <input type="checkbox" ng-model="activate" id="activate"> ACTIVATE <br />
    <input type="text" ng-model="first" ng-disabled="!activate">
    <input type="text" ng-model="second" ng-disabled="!activate">
    <!-- <span id="clickMe">Click Me!</span> -->
    <script type="text/javascript">
      app = angular.module('myApp', []);
      app.controller('myCtrl', function($scope, $timeout){

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

So this is the case. The other two fields will be enabled if the checkbox is checked and true and will be disabled if the checkbox is unchecked. What I want is, to make the fields go null or blank automatically upon disabling.

Here is an example: http://plnkr.co/edit/2EPuhRiR9OncV8z7q018?p=preview

1 Answer 1

2

Ignoring the fact that it is not a good practice to put too much logic directly in the view, a technical solution would be:

<input type="checkbox" ng-model="activate" id="activate" ng-change="sameAsAbove(first, second); value = null"> ACTIVATE <br />
<input type="text" ng-model="value.first" ng-disabled="!activate">
<input type="text" ng-model="value.second" ng-disabled="!activate">
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.