• Clever Geek Handbook

    How to add class to ng-repeat

    Here's my HTML:

    <div ng-controller="MainController">
      <ul class="list-holder">
         <li ng-repeat="check in checks" ng-class="{active : isSelected(check)}">
            <button ng-click="setcolor(check)">{{check.name}} </button> &nbsp;
            <button ng-click="setcolor(check)">{{check.name}} </button> &nbsp;
            <button ng-click="setcolor(check)">{{check.name}} </button> &nbsp;
            <hr>
        </li>
      </ul>
      <hr>
       {{selected | json}}
    </div>
    
          

    and this is my controller

    function MainController($scope) {
        $scope.checks = [
           {name: 'sports'},
           {name: 'movies'},
           {name: 'Others'}
        ];
    
        $scope.setcolor = function(chk) {
           $scope.selected = chk;
        }
    
        $scope.isSelected = function(check) {
           return $scope.check === check;
        }
    }
    
          

    THIS IS MY CSS file

    .active {
       background-color: red;
     }
    
          

    I want to select one button per row using a toggle function. Suppose the first row has button 1 and second row 2 and third row button 1 selected.

    +3
    javascript angularjs css angular-ui-bootstrap


    Abhishek 08 oct. 14 at 10:21
    source to share


    3 answers


    It looks like you have a small error in the function isSelected

    . You want to compare against a property $scope.selected

    :



    $scope.isSelected = function(check) {
        return $scope.selected === check;
    }
    
          

    +1


    dfsq 08 oct. 14 at 10:24
    source to share


    Actually I want something like this with ng-repeat ... @ Rohit Azad

    http://plnkr.co/edit/DvTcjGsN1Tnl8rleys1P?p=preview

    ** My HTML is **

     <html ng-app="ui.bootstrap.demo">
        <head>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script>
        <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.2.js"></script>
        <script src="example.js"></script>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" 
         rel="stylesheet">
       </head>
    <body>
       <div ng-controller="ButtonsCtrl">
    
         <h4>Radio &amp; Uncheckable Radio</h4>
         <pre>{{radioModel || ''}}</pre>
         <div class="btn-group, btn- toggle">
             <label class="btn btn-default" ng-model="radioModel" btn-radio="'Left'">Left</label>
            <label class="btn btn-default" ng-model="radioModel" btn-radio="'Middle'">Middle</label>
           <label class="btn btn-default" ng-model="radioModel" btn-radio="'Right'">Right</label>
        </div>
    
        </div>
    </body>
    </html>
    
          



    Here is my controller

    angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
    angular.module('ui.bootstrap.demo').controller('ButtonsCtrl', function ($scope) {
      $scope.singleModel = 1;
    
      $scope.radioModel = '';
    
     $scope.checkModel = {
          left: false,
          middle: false,
          right: false
     };
    });
    
          

    And here is my Css file

    .btn-toggle {
    height: 32px;
    border: 1px solid $basic-btn;
    border-radius: 4px;
    .btn-primary
    {
      background: $bg-white;
      border-right: 1px solid $basic-btn;
      color: $black;
      &:hover, &.active
    {
     background: $basic-btn;
     color: $bg-white;
     } 
    }
     .btn-primary:last-child
    {
     border-right: none;
    }
    .btn-primary:nth-child(2)
    {
      border-right: 2px solid $basic-btn;
      border-left: 1px solid $basic-btn;
    }
    }
    
          

    +1


    Abhishek 08 oct. 14 at 11:38
    source to share


    hi **@Abishek**

    I created a plunker with all the include code **@dfsq**

    answer

    I think you want this

    Angular code is

    var appfi = angular.module('firstApp', []);
    
    appfi.controller('MainController', function($scope){
        $scope.checks= [
           {'name': 'sports'},
           {'name': 'movies'},
           {'name': 'Others'}
        ];
    
    
        $scope.setcolor = function(chk) {
           $scope.selected = chk;
        };
    
        $scope.isSelected = function(check) {
        return $scope.selected === check;
        };
    
    } );
    
          

    Html code

        <body ng-app="firstApp">
        <h1>Hello Plunker!</h1>
    
        <div ng-controller="MainController">
      <ul class="list-holder">
         <li ng-repeat="check in checks" ng-class="{active : isSelected(check)}">
            <button ng-click="setcolor(check)">{{check.name}} </button> &nbsp;
            <button ng-click="setcolor(check)">{{check.name}} </button> &nbsp;
            <button ng-click="setcolor(check)">{{check.name}} </button> &nbsp;
            <hr>
        </li>
      </ul>
      <hr>
       {{selected | json}}
    </div>
      </body>
    
          

    Plunker

    0


    Rohit azad 08 oct. At 10:39
    source to share






    More articles:

    • ORA-00932: inconsistent data types: expected INTERVAL DAY SECOND received CHAR - oracle
    • HoughCircles circle detection using opencv and python- - python
    • HoughCircles cannot detect circles in this image - c ++
    • Android RTSP Streaming over TCP - android
    • Why Short.valueOf (n) Requires Listing - java
    • Index out of range when reading with Openpyxl - python
    • Column order in primary key, performance - performance
    • Android ORMlite SelectArg - android
    • Creating Recursive Template Blocks in Mojolicious - perl
    • Порядок столбцов MySQL в составном ключе - mysql

    All Articles

    Daily Blog | 2020

    Green Geek Media (GGM)
    1298 Despard Street
    GA 30344 East Point, USA
    404-763-3837