У меня есть простая директива youtube на одной странице и отлично работает. Однако, если я перейду на страницу 2, а затем вернусь, игрок больше не появится. Я что-то упустил, или просто не понимаю, как работают директивы?Почему директива не работает после смены страницы? Angular
Вот Директива
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"
})
Благодарит PLUNKER
!
Используйте https://github.com/brandly/angular-youtube-embed, и с вами все будет в порядке. – lin