jQuery Raty and AngularJS

陌路散爱 提交于 2020-01-13 18:09:02

问题


I'm trying to use jQuery Raty plugin with AngularJS. No success yet.

What I need is something like:

<ul>
    <li ng-repeat="obj in collection">
        <p>{{obj.rating}}</p>
        <div id="star{{$index}}"></div>

        <p>{{obj.someText}}</p>

        <script>$("#star{{$index}}").raty();</script>
    </li>
</ul>

But the script seems not to be executed and it doesn't even appear in the webpage. Besides it looks also like a very ugly approach, but I don't have more ideas.

How can I do this? Or do I need a different plugin / functionality for the stars?


回答1:


If I had to do this, I will develop an AngularJS directive, like this:

JS

yourApp.directive("raty", function() {
    return {
        restrict: 'AE',
        link: function(scope, elem, attrs) {
            $(elem).raty({score: attrs.score, number: attrs.number});
        }
    }
}

HTML

<raty id="star{{$index}}" score="1" number="5"></raty>

If you want add some parameters on Raty, you can edit simply my directive ;)




回答2:


You should be able to create a directive:

<div id="star{{$index}}", ng-stars>

Angular

.directive("ngStars", function() {
    return {
        restrict: 'A',
        link: function(scope, elem, attrs) {
            $(elem).raty();
        }
    }
}

Little new to Angular though, so, this may be the wrong thinking.



来源:https://stackoverflow.com/questions/18210159/jquery-raty-and-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!