angularjs 如何动态更改模板 url
angularjs how to change tempate url dynamically
我的模块中有一个指令。我想根据属性更改 templateUrl
。
HTML
<div test stage="dynamicstage"></div>
模块
angular.module('trial', [])
.controller('trialCtrl', function ($scope) {
$scope.dynamicstage = 'Welcome';
})
.directive('test', function () {
return {
restrict: 'A',
scope: {
'stage': '='
},
link: function (scope, element, attrs) {
scope.$watch('stage', function(condition){
if(stage === 'welcome'){
templateUrl: "hello.html";
}else{
different template url...
};
});
}
}
});
这不起作用。 templateurl
未加载到 div。我想动态更改 templateUrl 这可能吗?
感谢任何帮助。
这在 Angular 中不是很透明。 templateUrl
可以是动态构造模板 URL 的函数,但是在您的情况下,您需要一个作用域,在构造 URL 时尚不可用。
你可以在 ngInclude
的帮助下做这样的事情:
app.directive('test', function() {
return {
restrict: 'A',
scope: {
'stage': '='
},
template: '<div ng-include="templateUrl"></div>',
link: function(scope, element, attrs) {
scope.$watch('stage', function(condition) {
if (scope.stage === 'Welcome') {
scope.templateUrl = "hello.html";
} else {
scope.templateUrl = "other.html";
};
});
}
}
});
解决方案1:
scope.$watch('stage', function(condition){
var templateUrl;
if(stage === 'welcome'){
templateUrl = "hello.html";
} else{
templateUrl = "someothertemplate.html";
};
//load the template;
$http.get(templateUrl)
.then(function (response) {
// template is loaded.
// add it and compile it.
angular.element(element).html(response.data);
$compile(element.contents())(scope);
});
});
解决方案2:
使用 ng-include
<div test stage="dynamicstage">
<div ng-include="templateUrl"></div>
</div>
内部指令:
scope.$watch('stage', function(condition){
var templateUrl;
if(stage === 'welcome'){
templateUrl = "hello.html";
} else{
templateUrl = "someothertemplate.html";
};
scope.$parent.templateUrl = templateUrl; // make sure that templateUrl is updated in proper scope
})
我的模块中有一个指令。我想根据属性更改 templateUrl
。
HTML
<div test stage="dynamicstage"></div>
模块
angular.module('trial', [])
.controller('trialCtrl', function ($scope) {
$scope.dynamicstage = 'Welcome';
})
.directive('test', function () {
return {
restrict: 'A',
scope: {
'stage': '='
},
link: function (scope, element, attrs) {
scope.$watch('stage', function(condition){
if(stage === 'welcome'){
templateUrl: "hello.html";
}else{
different template url...
};
});
}
}
});
这不起作用。 templateurl
未加载到 div。我想动态更改 templateUrl 这可能吗?
感谢任何帮助。
这在 Angular 中不是很透明。 templateUrl
可以是动态构造模板 URL 的函数,但是在您的情况下,您需要一个作用域,在构造 URL 时尚不可用。
你可以在 ngInclude
的帮助下做这样的事情:
app.directive('test', function() {
return {
restrict: 'A',
scope: {
'stage': '='
},
template: '<div ng-include="templateUrl"></div>',
link: function(scope, element, attrs) {
scope.$watch('stage', function(condition) {
if (scope.stage === 'Welcome') {
scope.templateUrl = "hello.html";
} else {
scope.templateUrl = "other.html";
};
});
}
}
});
解决方案1:
scope.$watch('stage', function(condition){
var templateUrl;
if(stage === 'welcome'){
templateUrl = "hello.html";
} else{
templateUrl = "someothertemplate.html";
};
//load the template;
$http.get(templateUrl)
.then(function (response) {
// template is loaded.
// add it and compile it.
angular.element(element).html(response.data);
$compile(element.contents())(scope);
});
});
解决方案2: 使用 ng-include
<div test stage="dynamicstage">
<div ng-include="templateUrl"></div>
</div>
内部指令:
scope.$watch('stage', function(condition){
var templateUrl;
if(stage === 'welcome'){
templateUrl = "hello.html";
} else{
templateUrl = "someothertemplate.html";
};
scope.$parent.templateUrl = templateUrl; // make sure that templateUrl is updated in proper scope
})