简单的jQuery星级和AngularJs(ng-repeat)

时间:2015-05-10 04:58:14

标签: javascript jquery angularjs rating

哦,男孩,哦,男孩

我想使用这个插件 http://www.jqueryscript.net/other/Simple-jQuery-Star-Rating-System-For-Bootstrap-3.html 和angularjs一起。

只要我第一次加载页面时使用的是JSON,它就可以正常工作了。当我修改或甚至只是添加元素到数组时,星系统不再工作。到目前为止我花了很多时间来检测错误,但我无法解决。

这是推送新元素时的样子。

enter image description here

当我查看Google Chrome元素时,我发现新生成的星级评分缺少一堆代码。

首次加载页面时生成

<div class="star-rating rating-md rating-active"><div class="clear-rating clear-rating-active" title="Clear"><i class="glyphicon glyphicon-minus-sign"></i></div><div class="rating-container rating-gly-star" data-content=""><div class="rating-stars" data-content="" style="width: 142px;"></div><input value="4" type="number" class="rating form-control hide" min="0" max="5" step="0.5"></div><div class="caption"><span class="label label-primary">Four Stars</span></div></div>

按下按钮推送新元素后,新代码就像这样

<input value="4" type="number" class="rating" min="0" max="5" step="0.5">

html代码

 <div ng-repeat="x in allRecords"  >
                        <input id="input-21d" value="4" type="number" class="rating" min=0 max=5 step=0.5 data-size="sm">
                    </div>

Controller Init

Edi:我的控制器和模块设置如下所示,如果你回答,请考虑这个:)

var myApp  = angular.module('myApp', ['ngAnimate'])


 myApp.controller("ContactController", function ($scope,  $sce, $http, $timeout) {

$scope.allRecords = [
                 {
                     "Name" : "Alfreds Futterkiste",
                     "City" : "Berlin",
                     "Country" : "Germany"
                 },
                 {
                     "Name" : "Berglunds snabbköp",
                     "City" : "Luleå",
                     "Country" : "Sweden"
                 },
                 {
                     "Name" : "Magazzini Alimentari Riuniti",
                     "City" : "Bergamo",
                     "Country" : "Italy"
                 },
                 {
                     "Name" : "Wolski Zajazd",
                     "City" : "Warszawa",
                     "Country" : "Poland"
                 }
             ]
             ;


    $scope.addElements= function() {
    var obj = { Name: 'Hello world!' };
                $scope.allRecords.push(obj);
};

});

解决方案添加新元素后 我只是重新加载了star-rating.js

  $(function() {
                function load_script() {
                    $('#myscript').remove();
                    $.getScript("js/star-rating.js?s=", function() {
                        $('script:last').attr('id', 'myscript');                            
                    });
                }
                load_script();
            });

我想这不是最好的方法,但到目前为止,我不知道怎么做。 因为star-rating.js只有22kb大,所以不应该有任何性能问题。

编辑2 我切换到此星级。 http://rateit.codeplex.com/

但是&#34;刷新问题&#34;遗骸,这意味着我必须在这种情况下重新加载.js&#34; jquery.rateit.js&#34;。我可以忍受,如果我有更多的时间和耐心,我会做得更好:D

1 个答案:

答案 0 :(得分:1)

每当你添加新对象时,angularjs只是添加新的输入元素,jquery-rating不知道这一点,为了与angularjs集成,你必须创建一个新的指令,如下所示:

var myApp = angular.module('myApp', ['ngAnimate']);
myApp.controller("ContactController", function ($scope,  $sce, $http, $timeout) {
       $scope.allRecords = [
         {
             "Name": "Alfreds Futterkiste",
             "City": "Berlin",
             "Country": "Germany"
         },
         {
             "Name": "Berglunds snabbköp",
             "City": "Luleå",
             "Country": "Sweden"
         },
         {
             "Name": "Magazzini Alimentari Riuniti",
             "City": "Bergamo",
             "Country": "Italy"
         },
         {
             "Name": "Wolski Zajazd",
             "City": "Warszawa",
             "Country": "Poland"
         }
       ];

       $scope.addElements = function () {
           var obj = { Name: 'Hello world!' };
           $scope.allRecords.push(obj);
       }
   }).directive('myStart', function () {
       return function (scope, element, attrs) {
           $(element).rating();
       };
   });

和他们html(删除类评级,因此jquery-rating不会自动创建Rating):

<div ng-app="App1" ng-controller="Controller1">
<input ng-repeat="x in allRecords" my-start  id="rating-system" type="number"  min="1" max="5" step="1">
 <button ng-click="addElements()">Click me</button>

因此,每次创建输入标记时,我们都会调用$ .rating来应用于该新输入。