Angular 1.4.7中的角度星级评定指令问题

时间:2016-01-11 23:03:18

标签: angularjs angularjs-directive

我对星级评定指令有点问题。

这是星级评级指令代码,它在Angular 1.3.16版本中完美运行,但不适用于Angular 1.4.7版本。

实际上,它仍然在Angular 1.4.7版本中工作,但保持报告错误" TypeError:scope.onRatingSelected不是函数"

 .directive("starRating", function() {
      return {
        restrict : "EA",
        template : "<ul class='rating' ng-class='{readonly: readonly}'>" +
                   "  <li ng-repeat='star in stars' ng-class='star' ng-click='toggle($index)'>" +
                   "    <i class='fa fa-star'></i>" + //&#9733
                   "  </li>" +
                   "</ul>",
        scope : {
          ratingValue : "=ngModel",
          max : "=?", //optional: default is 5
          onRatingSelected : "&?",
          readonly: "=?"
        },
        link : function(scope, elem, attrs) {
          if (scope.max == undefined) { scope.max = 5; }
          function updateStars() {
            scope.stars = [];
            for (var i = 0; i < scope.max; i++) {
              scope.stars.push({
                filled : (i < scope.ratingValue.rating)
              });
            }
          };
          scope.toggle = function(index) {
            if (scope.readonly == undefined || scope.readonly == false){
              scope.ratingValue.rating = index + 1;
              scope.onRatingSelected({
                rating: index + 1
              });
              scope.onRatingSelected()(index + 1);
            }
          };
          scope.$watch("ratingValue.rating", function(oldVal, newVal) {
            if (newVal) { updateStars(); }
          });
        }
      };  
    })

0 个答案:

没有答案