我有一项声明:
$scope.showAlert = function() {
var alertPopup = $ionicPopup.alert({
title: 'Notification',
template: ''
);
$timeout(function() {
alertPopup.close(); //close the popup after 3 seconds for some reason
}, 30000);
};然后我有了这个:
if (!$scope.newDevice.name) {
$scope.showAlert.template = 'Name Required';
showAlert();
return;
}但在我宣布template为空后,我不知道该如何更新它。我试过:
$scope.showAlert.template = 'Name Required';或者是$scope.showAlert['template'] = 'Name Required';,但没能成功
发布于 2016-12-04 15:58:57
在您的代码中,template只是对象的一个属性。该对象的作用域是函数showAlert,因此不能访问它并从方法外部更新它。相反,您可以做的是在函数showAlert中引入模板参数,并在显示警报时使用它:
$scope.showAlert = function(alertTemplate) { // <- introduce parameter
if(alertTemplate === undefined) { // If parameter was not provided ...
alertTemplate = ''; // ... set it to empty string
}
var alertPopup = $ionicPopup.alert({
title: 'Notification',
template: alertTemplate // <- use parameter value
);
$timeout(function() {
alertPopup.close(); //close the popup after 3 seconds for some reason
}, 30000);
};然后你可以像这样使用它:
if (!$scope.newDevice.name) {
$scope.showAlert('Name Required');
return;
}在不需要提供自定义模板的情况下,可以简单地省略参数,然后使用空字符串:
$scope.showAlert();https://stackoverflow.com/questions/40960363
复制相似问题