使用指令渲染星级评定系统

时间:2016-01-18 13:10:01

标签: javascript angularjs ionic-framework

我正在尝试使用angularjs / ionic静态创建星级评分系统但收效甚微。但目前屏幕上没有任何内容输出......我做错了什么?

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);
});

1 个答案:

答案 0 :(得分:2)

<强> 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
     };

并修改 service.html

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

希望它会帮助你。!!