gpt4 book ai didi

javascript - 如何从 javascript 调用 AngularJS 函数?

转载 作者:行者123 更新时间:2023-12-03 12:29:33 26 4
gpt4 key购买 nike

我试图在 if 语句之后从 Javascript 调用 AngularJS 方法 advanceSlide(),但是,这一行:

angular.element(document.getElementById('FotoSpill')).scope().advanceSlide();

似乎不起作用。这是完整的代码。

Javascript

    window.onload = function() {
cast.receiver.logger.setLevelValue(0);
window.castReceiverManager = cast.receiver.CastReceiverManager.getInstance();
console.log('Starting Receiver Manager');

// handler for the CastMessageBus message event
window.messageBus.onMessage = function(event) {
console.log('Message [' + event.senderId + ']: ' + event.data);
// display the message from the sender
displayText(event.data);
if (event.data == "quit") {
angular.element(document.getElementById('FotoSpill')).scope().advanceSlide();
};
// inform all senders on the CastMessageBus of the incoming message event
// sender message listener will be invoked
window.messageBus.send(event.senderId, event.data);
}

ANGULARJS

var FotoSpill = angular.module('FotoSpill', []);
FotoSpill.config(['$routeProvider', '$locationProvider', function( $routeProvider, $locationProvider ) {$routeProvider.when('/tag/:tag');}]);
FotoSpill.controller('slideshow', function ( $scope, $http, $timeout, $route, $location ) {
// Set the API endpoint
var api = 'https://api.instagram.com/v1/locations/436022/media/recent?access_token=257058201.9af4692.3d68e63b114944a0be332da732923a23&callback=JSON_CALLBACK',
newReq, refreshApi;
var seconds = 1000;


$scope.fetchImages = function() {

$scope.loadingClass = 'loading';
$scope.imgCurrent = 0;


// if ( ! $route.current )
// $location.path( '/tag/' + $scope.tag );
// else if ( angular.isDefined( $route.current.params.tag ) )
// $scope.tag = $route.current.params.tag;

$http.jsonp(
api.replace( '%tag%', $scope.tag )
).success( function( data ) {
delete $scope.loadingClass;

$scope.images = data.data;

// Set the first image active
if ( data.data.length )
$scope.makeActiveSlide( $scope.imgCurrent );

// Cancel the previous update request
if ( refreshApi )
$timeout.cancel( refreshApi );

// Check for new images on every loop
if ( data.data.length )
refreshApi = $timeout( $scope.fetchImages, 60*seconds );
}).error( function() {
delete $scope.loadingClass;
refreshApi = $timeout( $scope.fetchImages, 2*seconds );
});
}

// Fetch images
$timeout( $scope.fetchImages );

$scope.advanceSlide = function() {
// Method 1
// Use a classname to highlight the current active slide
if ( angular.isDefined( $scope.images ) && $scope.images.length )
$scope.makeActiveSlide( $scope.imgCurrent + 1 );


$timeout( $scope.advanceSlide, 6*seconds ); //time between slide transition
}

}
).filter(
'escape', function () {
return function( input ) {
return escape( input );
}
}
);

最佳答案

您需要应用更改

angular.element(document.getElementById('FotoSpill')).scope().$apply('$scope.advanceSlide()');

尝试一下

关于javascript - 如何从 javascript 调用 AngularJS 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24005029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com