methuselah
methuselah

Reputation: 13216

Rendering a star rating system using directives

I am trying to create a star rating system statically using angularjs/ionic with little success. But currently nothing is output to the screen... what I am I doing wrong?

service.html

  <ion-list>
    <ion-item ng-repeat="business in businessList track by $index" class="item-icon-right">
      <h2>{{business.name}}</h2> {{business.distance}} miles
      <br>
      <div star-rating rating-value="{{business.rating}}" max="rating.max"></div>
      <i class="icon ion-chevron-right icon-accessory"></i>
    </ion-item>
  </ion-list>

directives.js

angular.module('starter.directives', [])

.directive('starRating', function() {
  return {
    restrict: 'A',
    template: '<ul class="rating">' +
      '<li ng-repeat="star in stars" ng-class="star">' +
      '\u2605' +
      '</li>' +
      '</ul>',
    scope: {
      ratingValue: '=',
      max: '='
    },
    link: function(scope, elem, attrs) {
      scope.stars = [];
      for (var i = 0; i < scope.max; i++) {
        scope.stars.push({
          filled: i < scope.rating
        });
      }
    }
  }
});

services.js

.service("BusinessData", [function () {
    var businessData = [
    {
        id: 1,
        serviceId: 1,
        name: 'World Center Garage',
        distance: 0.1,
        rating: 4
    }
];

    return {
        getAllBusinesses: function () {
            return businessData;
        },

        getSelectedBusiness: function(serviceId) {
            var businessList = [];
            serviceId = parseInt(serviceId);
            for(i=0;i<businessData.length;i++) {
                if(businessData[i].serviceId === serviceId) {
                    businessList.push(businessData[i]);
                }
            }
            return businessList;
        }
    }
}])

controller.js

.controller('ServiceCtrl', function($scope, ServicesData, BusinessData, $stateParams) {
  $scope.service = ServicesData.getSelectedService($stateParams.service);
  $scope.businessList = BusinessData.getSelectedBusiness($stateParams.service);
});

Upvotes: 0

Views: 290

Answers (1)

Kunal Kakkad
Kunal Kakkad

Reputation: 653

controller.js

.controller('ServiceCtrl', function($scope, ServicesData, BusinessData, $stateParams) {
  $scope.service = ServicesData.getSelectedService($stateParams.service);
  $scope.businessList = BusinessData.getSelectedBusiness($stateParams.service);
  $scope.ratings = {
      current: 5,
      max: 10
     };

And also modify service.html

<div star-rating rating-value="rating.current" max="rating.max"></div>

Hope it will help you out.!!

Upvotes: 2

Related Questions