Javascript AngularJS-带有templateUrl函数refresh/redraw/update的指令
我有一个angularJS指令,它的templateUrl属性设置为一个函数,它根据服务函数确定要使用哪个模板。当服务完成某个作业(该作业也是由指令内的事件触发的)时,它会触发回调,该回调修改指令的范围,并应调用指令的“重画”。重画是指调用templateUrl函数,并根据服务中的新值正确更新模板。我不知道如何称呼这个重画/更新,谷歌对我的问题也没有用Javascript AngularJS-带有templateUrl函数refresh/redraw/update的指令,javascript,html,angularjs,angularjs-directive,Javascript,Html,Angularjs,Angularjs Directive,我有一个angularJS指令,它的templateUrl属性设置为一个函数,它根据服务函数确定要使用哪个模板。当服务完成某个作业(该作业也是由指令内的事件触发的)时,它会触发回调,该回调修改指令的范围,并应调用指令的“重画”。重画是指调用templateUrl函数,并根据服务中的新值正确更新模板。我不知道如何称呼这个重画/更新,谷歌对我的问题也没有用 application.directive('loginform', ['authorization', function(auth) {
application.directive('loginform', ['authorization', function(auth) {
return {
restrict: 'E',
transclude: true,
replace: true,
templateUrl: function(element, attributes)
{
if(auth.token() != "")
{
return "/client/templates/loginForm_disabled.html";
}
else
{
return "/client/templates/loginForm_enabled.html";
}
},
link: function($scope, element, attributes) {
$scope.username = "";
$scope.password = "";
$scope.message = "";
//this function is called on a button press.
$scope.login = function() {
auth.login($scope.username, $scope.password, function (success) {
if(!success)
{
$scope.message = "Login was unsuccessful";
}
else
{
$scope.message = "";
}
//call a redraw/update of the template
});
}
}
}
}]);
尝试使用$scope.$apply()
这些问题的细节可能会有所帮助
为什么不能在模板中简单地使用ng if或ng show指令?它可能类似于:
禁用的表单
。然后处理它。ng-if创建一个子范围。如果可能的话,我想避免这种情况。如果templateUrl更新没有好的解决方案,那么我将使用ng ifs。