【问题标题】:Rendering a star rating system using directives使用指令渲染星级系统
【发布时间】:2016-01-19 11:40:06
【问题描述】:

我正在尝试使用 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);
});

【问题讨论】:

  • 您能否告诉我们您的控制台中是否显示任何内容?一切都加载正确吗?会发生什么,什么不会发生?
  • 如果我用{{business.rating}} out of 5 stars 替换&lt;div star-rating rating-value="{{business.rating}}" max="rating.max"&gt;&lt;/div&gt;,那么我会得到正确的输出(即在这种情况下,5 颗星中有 4 颗星)。我想问题是协调指令对该值的操作,以及 service.html 上的最终输出
  • 你能不能解决这个问题
  • 什么是“rating.max”?看来你从来没有设置过
  • @KunalKakkad 我不确定我将如何去做,但我将代码库基于在此问题中找到的解决方案:stackoverflow.com/questions/23646395/…

标签: javascript angularjs ionic-framework


【解决方案1】:

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>

希望能帮到你!!

【讨论】:

  • 这是不正确的,因为范围内的 rating 对象是一个列表,并且在 html 代码中您引用了一个 rating 对象,该对象尚未添加到控制器中。
  • 嘿,请您现在检查一下,因为我已经在 controller.js 中更新了我的答案
猜你喜欢
  • 2014-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-11
  • 2010-11-04
  • 2013-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多