0

Trying to implement multiple <input /> fields, that populate <div> elements with the text entered. After those are implemented, I want to implement a set of <input class="checkbox"> that when clicked will wrap the input text in additional text, depending on the checkbox selected.

The problem is the 1st checkbox is bound to the second checkbox. How do I separate the model associated with each checkbox?

I am currently using ng-switch-when and trying to set the values in the controller, but it's failing to bind separately as per the plunkr

<div class="code alert-box warning" ng-switch on="TrackingCode.AddOnClick">
<div ng-switch-when="true">
    <!-- code to render when AddOnClick checkbox is checked-->
    <code>
        onclick="_gaq.push(['_trackEvent({{category}}', '{{action}}', '{{label}}', '{{value}}', '{{nonInteraction}}']);"
    </code>
</div>
<div ng-switch-when="test">
    <!-- code to render when Something Else checkbox is checked-->
    <code>
        SomethingElse="_gaq.push(['_trackEvent({{category}}', '{{action}}', '{{label}}', '{{value}}', '{{nonInteraction}}']);"
    </code>
</div>
<div ng-switch-default>
    <!-- code to render when AddOnClick checkbox is un-checked -->
    <code>
        _gaq.push(['_trackEvent', '{{category}}', '{{action}}', '{{label}}', '{{value}}', '{{nonInteraction}}']);
    </code>
</div>
</div> 
1
  • You assign the same model to both checkboxes, so they show the same values. Seems you are looking for radio buttons, at least in functionality. Commented Feb 22, 2015 at 1:53

1 Answer 1

1

First off, you can simplify your OnClickCheckbox() function to:

 /* Wrap output in onclick function */
$scope.OnClickCheckbox = function () 
    $scope.AddOnClick = !($scope.AddOnClick);
}

Secondly, you lack a UniversalGACheckbox() function, which you are assigning to your second checkbox ng-click.

Third, if you are assigning an ng-switch-when=test, that will never happen if you are binding to a value that only alternates between true and false, that state will never be reached.

You need to separate your checkboxes so that they are not bound to the same true/false $scope variable, although I think there is probably a better way to do this, a quick fix would be something like:

<div class="code alert-box warning" ng-switch on="TrackingCode.AddOnClick">

                <div ng-switch-when="true">
                    <!-- code to render when AddOnClick checkbox is checked-->
                    <code>
                    onclick="_gaq.push(['_trackEvent({{category}}', '{{action}}', '{{label}}', '{{value}}', '{{nonInteraction}}']);"
                    </code>            
                </div>
                <div ng-switch-default>
                    <!-- code to render when AddOnClick checkbox is un-checked -->
                    <code>
                    _gaq.push(['_trackEvent', '{{category}}', '{{action}}', '{{label}}', '{{value}}', '{{nonInteraction}}']);
                    </code>
                </div>
            </div>
            <div class="code alert-box warning" ng-switch on="TrackingCode.AddOnClick2">
              <div ng-switch-when="true">
                    <!-- code to render when AddOnClick checkbox is checked-->
                    <code>
                    SomethingElse="_gaq.push(['_trackEvent({{category}}', '{{action}}', '{{label}}', '{{value}}', '{{nonInteraction}}']);"
                    </code>            
                </div>
                <div ng-switch-default>
                    <!-- code to render when AddOnClick checkbox is un-checked -->
                    <code>
                    _gaq.push(['_trackEvent', '{{category}}', '{{action}}', '{{label}}', '{{value}}', '{{nonInteraction}}']);
                    </code>
                </div>
            </div>

And in your script:

/* Wrap output in onclick function */
    $scope.OnClickCheckbox = function () {

        $scope.AddOnClick = !($scope.AddOnClick);
    }

    $scope.UniversalGACheckbox = function(){
      $scope.AddOnClick2 = !($scope.AddOnClick2);
    }

However, this is bad coding style, and involves a lot of duplication

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

1 Comment

Thanks, I never thought about duplicating the HTML - get that it's bad practice but I'm just keen to get this working and can tidy it up later when I understand this stuff a bit more. Will use ng-show to control the display of those duplicated divs.

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.