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

标签 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 初始化

Edi:我的 Controller 和模块设置是这样的,如果你回答请考虑一下:)

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/

但是“刷新问题”仍然存在,这意味着在本例中我必须重新加载 .js“jquery.rateit.js”。如果我有更多的时间和耐心,我可以忍受,我会让它变得更好 :D

最佳答案

每次添加新对象时,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 以应用于该新输入。

关于javascript - 简单的 jQuery 星级评分和 AngularJs (ng-repeat),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30147895/

相关文章:

javascript - 给定一个 html 字符串,在 javascript 中仅删除 anchor HTML 标签的最佳方法是什么?

jquery - 单击文档上的其他位置时,框消失了? (jQuery)

jquery - 设置列表的背景图片

javascript - 无法读取未定义(…)的属性 '_aData' - 数据表

angularjs - 匹配所有的 Play Framework 路线

javascript - 等待 AngularJS $http 中的响应

angularjs - 如何创建 Angular.js $resource/$http 工厂服务来处理来自外部资源的具有多个参数的查询字符串?

javascript - 如何在动态添加的内容中绑定(bind)knockout.js中的事件?

javascript - 通过 PushState 和 Jekyll 使用 AJAX 加载

javascript - Raphael.js - 向元素注册多个事件