how to create up/down voting function in angularjs

后端 未结 2 1361
不知归路
不知归路 2021-02-11 03:12

Hey guys may i know how can i create a up/down voting function in angularjs ? i want to make it to something similar to stackoverflow or reddit. i found a Jquery plugin https:/

相关标签:
2条回答
  • 2021-02-11 03:22

    Just use a single scope variable to act like a toggle button.

    Sample Demo: http://plnkr.co/edit/C4w9hDK3xW1R0ua3WPgU?p=preview

    HTML:

    <body ng-controller="MainCtrl">
      <i title="Up Votes" ng-click="changeVote(vote, 'up')" class="fa fa-arrow-circle-up fa-2x" ng-class="{true:'up', false:''}[vote=='up']"></i>
      <br>
      <i title="Down Votes" ng-click="changeVote(vote, 'down')" class="fa fa-arrow-circle-down fa-2x"  ng-class="{true:'down', false:''}[vote=='down']"></i>
      <br>Vote: {{vote}}
    

    Controller Logic:

    app.controller('MainCtrl', function($scope) {
      $scope.changeVote = function(vote, flag) {
        $scope.vote = vote == flag ? 'None' : flag;
        alert($scope.vote);
      };
    });
    
    0 讨论(0)
  • 2021-02-11 03:25

    It should be simple to make something like this.
    Here's a simple example:

    JS:

    var app = angular.module('voteApp', []);
    
    app.controller('MainCtrl', function ($scope) {
        $scope.upVote = function () {
            $scope.vote++;
        }
    
        $scope.downVote = function () {
            $scope.vote--;
        }
    
        $scope.vote = 0;
    });
    

    Fiddle.

    0 讨论(0)
提交回复
热议问题