简体   繁体   English

插件的Angular和jQuery动态绑定

[英]Angular and jquery dynamic binding of plugin

I have a sample APP, where I need to rotate say a list of todo Items in Angular. 我有一个示例APP,我需要在其中旋转说出Angular中的待办事项列表。 so instead of display the todos in a list, I want just one item at a time and rotate the rest one by one. 因此,我不想一次在列表中显示待办事项,而是一次只想要一项,然后将其余一项逐一旋转。 I am using jquery cycle plugin to acheive the effect, since the todos are getting added at run time, the jquery cycle plugin isn't picking the changes. 我正在使用jquery cycle插件来达到效果,因为待办事项是在运行时添加的,因此jquery cycle插件不会选择所做的更改。 Any suggestions how to acheive this. 任何建议如何实现这一目标。

I have put up a sample Demo 我已经提供了一个示例演示

    var app = angular.module('App', []);
app.controller('AppCtrl', function($scope) {
  $scope.guys = ['Mike','Zerrra','Rick','Tom','Silva','Pollo'];
  $scope.addPerson = function() {
    $scope.guys.push($scope.newPerson);
    $scope.newPerson = '';
  }
});


app.directive('rp', function() {
  return {
    restrict: 'A',
    link:function(scope, elem, attrs) {
      $(elem).cycle({
                fx: 'scrollHorz',
                speed: 'slow',
                timeout: 1000,
                prev: '.prev',
                next: '.next'
            }); 
    }
  }
})

http://jsfiddle.net/WK2Fg/119/ http://jsfiddle.net/WK2Fg/119/

Here is jsfiddle of my solution. 这是我的解决方案的jsfiddle。 Is that what you were looking for? 那是您要找的东西吗?

http://jsfiddle.net/WK2Fg/126/ http://jsfiddle.net/WK2Fg/126/

var app = angular.module('Application', []);
app.controller('AppCtrl', function($scope) {
    debugger;
  $scope.guys = ['Mike','Zerrra','Rick','Tom','Silva','Pollo'];
  $scope.addPerson = function() {
      debugger;
    $scope.guys.push($scope.newPerson);
    $scope.newPerson = '';
  }
});


app.directive('rp', function() {
  return {
    restrict: 'A',
    scope: { guys: "=" },
    link: function (scope, elem, attrs){
      scope.$watch('guys', function(value) {
          debugger;
          $(elem).empty();

          $.each(value, function(){
              var guy = this;
              $(elem).append("<li>"+ guy +"</li>");
          });

          $(elem).cycle({
                fx: 'scrollHorz',
                speed: 'slow',
                timeout: 1000,
                prev: '.prev',
                next: '.next'
            });
      }, true);
    }
  }
})

HTML: HTML:

  <div class="container" ng-app="Application">
    <div class="row" ng-controller="AppCtrl">
      <a href="#" class="left">Left</a>
        <ul class="middle" guys="guys"  rp ></ul>
      <a href="#" class="right">Right</a>
        <input type="text" ng-model="newPerson" />
      <button ng-click="addPerson()">Add Person</button>
    </div>
  </div>

PS PS

I am not very familiar with the jQuery plugin you are using to do the cycling, so currently I reset it after adding new elements. 我对用于循环的jQuery插件不是很熟悉,因此目前我在添加新元素后将其重置。 If this plugin has some functions that add new elements on the go, you can change the watch, to achieve smoother experience. 如果此插件具有一些可随时随地添加新元素的功能,则可以更换手表,以获得更流畅的体验。 I hope basing on my answer you'll figure out the rest. 我希望您能根据我的回答找出其余的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM