2015-04-28 37 views
2

是否有方法僅在某個Angularjs控制器/視圖中調用jQuery函數? 例如,我只想在主視圖中調用幻燈片功能。通過角度視圖調用jQuery函數

幻燈片:

$('.slides').superslides(); 

app.js:

var myApp = angular.module('myApp', ['ngRoute']); 
myApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) { 
$routeProvider 
.when('/', { 
    templateUrl : 'views/home.html', 
    controller : 'mainController' 
}); 

$locationProvider.html5Mode(true).hashPrefix('!'); 

}]); 

myApp.controller('mainController', function($scope, $location, $anchorScroll) { 
    $scope.scrollTo = function(id) { 
    $location.hash(id); 
    $anchorScroll(); 
    } 
}); 

回答

3

創建一個指示。

JS:

myApp.directive("superslides", 
     function() 
     { 
      return { 
       restrict: "A", 
       scope: {}, 
       link: function (scope, element, attrs) 
       { 
        $(element).superslides(); 
       } 
      } 
     } 
    ) 

HTML:

<ul superslides> 
</ul> 
+0

無需分離範圍會更好 – floribon