저는 resize 이벤트로 templateUrl을 변경하는 방법을 찾고있었습니다."more then one"의 templateUrl을 AngularJS의 지시어로 변경하십시오.
Change the templateUrl of directive based on screen resolution AngularJS
씨에 의해 게시 됨 : 나는 링크에서 답을 찾아 냈다. "Dfsq". 같은 시간에 두 개의 서로 다른 템플릿 URL에서 동일한 작업을 수행해야 할 때까지 제대로 작동했습니다. 작동하지 않았다. 지시문을 삭제하면 다른 지시문이 삭제됩니다.
내가 잘못된 부분이 있습니다.
플 렁커가 뒤 따른다.
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
});
angular.module('plunker')
.directive('browseContent', function($window) {
return {
restrict: 'E',
template: '<div ng-include="templateUrl"></div>',
link: function(scope) {
$window.onresize = function() {
changeTemplate();
scope.$apply();
};
changeTemplate();
function changeTemplate() {
var screenWidth = $window.innerWidth;
if (screenWidth < 768) {
scope.templateUrl = 'browse-content-mobile.html';
} else if (screenWidth >= 768) {
scope.templateUrl = 'browse-content.html';
}
}
}
}
})
.directive('segundoContent', function($window) {
return {
restrict: 'E',
template: '<div ng-include="templateUrl2"></div>',
link: function(scope) {
$window.onresize = function() {
changeTemplate2();
scope.$apply();
};
changeTemplate2();
function changeTemplate2() {
var screenWidth = $window.innerWidth;
if (screenWidth < 768) {
scope.templateUrl2 = 'segundoContent.html';
} else if (screenWidth >= 768) {
scope.templateUrl2 = 'segundoContent-mobile.html';
}
}
}
}
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link rel="stylesheet" href="style.css" />
<script data-require="[email protected]" src="https://code.angularjs.org/1.2.22/angular.js" data-semver="1.2.22"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<browse-content></browse-content>
<segundo-content></segundo-content>
</body>
</html>
추가에게 방법. 둘 다 작동을 멈췄다. 추가 할 장소는 무엇입니까? 도움 주셔서 감사합니다. sr. Pankaj – Lovera
의사 소통을하는 동안 영어로 할 수 있겠습니까? –
@ user3403205 업데이트 된 코드 및 plunkr도 확인하십시오. Thanks :) –