0
템플릿 URL이있는 문자열 객체를 지시문에 전달할 수있는 방법이 있습니까? 내 솔루션이 올바르게 작동하지 않습니다.angularjs 지시문에 템플릿 URL 전달
자바 스크립트 :
function moduleView() {
return{
restrict: 'A',
templateUrl: function (elem, attr) {
return attr.url;
}
};
};
function ModulesCtrl($scope, ModulesService) {
$scope.modules = [];
$scope.run = function() {
ModulesService.loadModules(function (items) {
$scope.modules = items;
});
};
$scope.run();
};
HTML (옥) :
ul(ng-repeat="item in modules")
div(module-view url="item.path")
item.path 템플릿 오 URL을 문자열입니다.
하지만이 코드는 작동하지 않습니다. 나는이 오류가 :
Error: [$compile:tpload] Failed to load template: item.path
'return attr.url;'는'path' 속성에 저장된 URL이 아닌'item.path' 문자열을 반환합니다. 반환하기 전에 문자열을 보간해야합니다. 문제는 보간이 일어나기 전에'templateUrl' 함수가 실행된다는 것입니다. 또한'$ scope '도 사용할 수 없으며'$ rootScope' 만 사용할 수 있습니다. '$ rootScope'에서 항목을 참조합니까? – tasseKATT
제어기'function의 $ scope에서 참조한 항목 ModulesCtrl ($ scope, ModulesService) { $ scope.modules = []; $ scope.run = function() { ModulesService.loadModules (function (items) { $ scope.modules = items; }); }; $ scope.run(); }'item은 배열 모듈의 요소입니다. ul (ng-repeat = "모듈의 항목")' – Egor
템플릿 : "
" – harishr