在Angular完成呈现

时间:2016-03-06 23:50:06

标签: javascript jquery angularjs json ionic-framework

我对Angular JS很新,因此这个问题可能真的很愚蠢。关于SO的几个问题也讨论了它,但是我无法做出任何改变。

我试图做一个看似非常简单的事情,但这一直非常困难。我试图在Angular呈现HTML之后运行JQuery代码。

我已经尝试了超过10个小时,但没有任何作用,请帮助!

这是JQuery代码: -

$(".scrollable-quotes").slick({
    infinite: true,
    speed: 500,
    fade: true,
    cssEase: 'linear'
});

它基本上运行了我的index.html中包含的滑块库。我的index.html如下: -

<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title>My Ang App</title>

    <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="scripts/slick/slick.min.js"></script>

  </head>

  <body ng-app="EBSheadlessDrupal" onload="StartMove()">
    <ion-nav-view></ion-nav-view>

    <!-- build:js scripts/vendor.js -->
    <!-- <script src="vendor/someContribJs.js"></script> -->
    <!-- bower:js -->
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/angular-animate/angular-animate.js"></script>
    <script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
    <script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
    <script src="bower_components/collide/collide.js"></script>
    <script src="bower_components/ionic/release/js/ionic.js"></script>
    <script src="bower_components/ionic/release/js/ionic-angular.js"></script>
    <!-- endbower -->
    <!-- endbuild -->

    <!-- build:js scripts/scripts.js -->
      <script src="scripts/app.js"></script>
      <script src="scripts/services.js"></script>
      <script src="scripts/controllers.js"></script>
    <!-- endbuild -->

</body>
</html>

这是我的controllers.js

'use strict';
angular.module('EBSheadlessDrupal.controllers', [])


// Controller that pulls events list from our services.
.controller('EventIndexCtrl', function($scope, EventService) {

    EventService.setups().then(function(setups){
        $scope.setups = setups.data;
        //console.log($scope.setups);
    });
})


// Controller that pulls events list from our services, and binds it to an individual view for display on the detail page.
.controller('EventDetailCtrl', function($scope, $stateParams, EventService) {

    var id = $stateParams.id;

    EventService.setups().then(function(setups){
        EventService.setup(setups.data,id, function(setup){
            $scope.setup = setup;
            //console.log($scope.setup);
            });
        });
    })

    // Controller that pulls events list from our services.
.controller('WhatIndexCtrl', function($scope, WhatService) {

    WhatService.whats().then(function(whats){
        $scope.whats = whats.data;
        //console.log($scope.whats);
    });
})


// Controller that pulls events list from our services, and binds it to an individual view for display on the detail page.
.controller('WhatDetailCtrl', function($scope, $stateParams, WhatService) {

    var id = $stateParams.id;

    WhatService.whats().then(function(whats){

        angular.element(document).ready(function () {
            document.getElementById('body').addClass("asdasdasd");

        });

        WhatService.what(whats.data,id, function(what){
            $scope.what = what;
            console.log($scope.what);
            });
        });
    })

// Controller that pulls single node JSON from our services, and binds to about.
    .controller('UserCtrl', function($scope, UserService) {

    UserService.user().then(function(user){
        $scope.user = user.data;
        //console.log($scope.user);
    });
})

// Controller that pulls single node JSON from our services, and binds to about.
    .controller('NodeCtrl', function($scope, NodeService) {

    NodeService.node().then(function(node){
        $scope.node = node.data;
        //console.log($scope.node);
    }, function(err) {
        alert(err.status + ' ' + err.statusText);
    });
})

//user login
.controller('AppCtrl', function() {
});

WhatService.whats().then(function(whats)需要它。这是我在函数中尝试的,但它不起作用: -

angular.element(".scrollable-quotes").slick({
              infinite: true,
              speed: 500,
              fade: true,
              cssEase: 'linear'
});

2 个答案:

答案 0 :(得分:1)

Angular.element正在使用jqlite,而不是jQuery。我不熟悉光滑,但是您尝试执行这样的代码:$(“。scrollable-quotes”)。slick ...

基本上就像你在第一个代码块中说明的一样。如果您在包含的脚本中使用了完整的jQuery,那么应该为完整的jQuery定义$。

答案 1 :(得分:1)

这需要在指令中完成,因此您可以确保在运行代码时元素存在。当在angular.js之前的页面中加载jQuery.js时,链接函数中的element是一个jQuery对象

模板中的某处:

<div slick >Slick content</div>

指令

angular.module('myMainApp', function(){
   return {
      restrict:'A',
      link:function(scope, element, attrs){
         element.slick({
           infinite: true,
           speed: 500,
           fade: true,
           cssEase: 'linear'
        });
      }
   }    
});

如果您需要时间在视图中呈现数据,首先可以在其中注入$timeout nd wrap初始化代码,以便将其推送到摘要周期结束