mardi 3 mai 2016

Change Text Label when the checkbox is checked or unchecked

So I am working on an AngularJS application and I am trying to update the checkbox label, everytime the user clicks on the input. The problem here is that my code works well on codepen.io, but when I import it into my main application, the label doesn't update when the value of the checkbox changes.

Below is my code and a link to a working demo on Codepen

HTML Markup

<form name="myForm" ng-app="follow.controllers" ng-controller="followCtrl">
<input type="checkbox" name="checkbox-follow-btn" id="checkbox-button-follow-user" class="hide-checkbox-follow-btn"
       ng-model="followUser.value" 
       ng-true-value="'UNFOLLOW'" 
       ng-false-value="'FOLLOW'">

<label for="checkbox-button-follow-user"></label>
</form>

CSS Styles

input[type=checkbox] {
    display: none;
}
input.hide-checkbox-follow-btn:checked + label:before {
content: "";
}

input.hide-checkbox-follow-btn:checked + label:after {
content: "";
}

input.hide-checkbox-follow-btn + label:before {
content: "";
}

.hide-checkbox-follow-btn + label {
  display: block;
  float: left;
  width: 150px;
  height: 30px;
  line-height: 30px;
  padding: 5px 0;
  text-align: center;
  background-color: #111;
  font-family: 'Lato', sans-serif;
  color: #fff;
  border-radius: 3px;
  /*border: 1px solid transparent;*/
  -webkit-transition: all 0.3s ease-in-out;
  -moz-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
  opacity: 0.3;
  box-shadow:0 0px 0px transparent;
}

.hide-checkbox-follow-btn:checked + label {
  display: inline-block;
  background-color: #fff;
  color: #111;
  /*border: 1px solid #fff;*/
  opacity: 1;
  box-shadow:0 2px 5px #111;
}

.hide-checkbox-follow-btn:checked + label + p {
  color: #fff;
}

My AngularJS Script

angular.module('follow.controllers', [])
      .controller('followCtrl', ['$scope', function($scope) {

        $scope.followUser = {
          value:"FOLLOW"
        };
      }]);

And by the way, I that UI Bootstrap is an option, but I would like to know that there is a more cleaner and simpler solution for this.

Please, help me figure it out. Thanks,

Aucun commentaire:

Enregistrer un commentaire