通过角度视图调用jQuery函数

时间:2022-10-10 19:44:19

Is there a way to call a jQuery function only in a certain Angularjs controller/view? For example, I want to call the slides function only in the home view.

是否有一种方法只在某个Angularjs控制器/视图中调用jQuery函数?例如,我只想在home视图中调用slide函数。

Slides:

幻灯片:

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

app.js:

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();
  }
 });

1 个解决方案

#1


3  

Create a directive.

创建一个指令。

JS:

JS:

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

html:

html:

<ul superslides>
</ul>

#1


3  

Create a directive.

创建一个指令。

JS:

JS:

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

html:

html:

<ul superslides>
</ul>