我在一個頁面上有一個簡單的youtube指令,工作正常。但是,如果我進入第2頁然後回來,玩家不會再出現。 我在這裏錯過了什麼,或者我只是誤解指令如何工作?爲什麼指令在換頁後不工作? Angular
這裏是PLUNKER
指令
app.directive('youtube', function($window) {
return {
restrict: "E",
template: '<div></div>',
link: function(scope, element, attrs) {
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
$window.onYouTubeIframeAPIReady = function() {
player = new YT.Player(element.children()[0], {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE'
});
};
},
}
});
控制器和應用路線
app.config(['$locationProvider', '$routeProvider',
function($locationProvider, $routeProvider) {
$routeProvider.when('/view1', {
templateUrl: 'page1.html',
controller: 'firstCtrl'
});
$routeProvider.when('/view2', {
templateUrl: 'page2.html',
controller: 'secondCtrl'
});
$routeProvider.otherwise({redirectTo: '/view2'});
}])
app.controller('mainCtrl', function($scope) {
$scope.title = "Main Page"
})
app.controller('firstCtrl', function($scope) {
$scope.title = "Page 1"
})
app.controller('secondCtrl', function($scope) {
$scope.title = "Page 2"
})
謝謝!
使用https://github.com/brandly/angular-youtube-embed,你會沒事的。 – lin