Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have this declaration:

$scope.showAlert = function() {
    var alertPopup = $ionicPopup.alert({
        title: 'Notification',
        template: ''
    );
    $timeout(function() {
        alertPopup.close(); //close the popup after 3 seconds for some reason
                }, 30000);
};

then I have this:

      if (!$scope.newDevice.name) {
            $scope.showAlert.template = 'Name Required';
            showAlert();
            return;
        }

But I don't know exactly how to update template after I`ve declared it empty. I have tried:

$scope.showAlert.template = 'Name Required';

or $scope.showAlert['template'] = 'Name Required'; but couldn't`t make it

share|improve this question

In your code template is just a property of an object. The scope of this object is the function showAlert, so you cannot access it and update it from outside the method. What you can do instead is to introduce template parameter to function showAlert, and use it when displaying alert:

$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);
};

And then you can use it like this:

if (!$scope.newDevice.name) {
    $scope.showAlert('Name Required');
    return;
}

In cases where you don't need to provide custom template, you can simply omit the parameter, and empty string will be used:

$scope.showAlert();
share|improve this answer
    
if I add $scope.showAlert('anything'); it will pop-out 2 times this allert. once without and once with the Template – Xao 20 hours ago
    
I have to add it showAlert('anything'); but this doesnt pass the param – Xao 20 hours ago
    
@Xao You're not showing some code. If you call showAlert() like you did in your sample it should throw an error, because function showAlert does not exist (or at least you are not showing it). And if no other alert function exists on $scope I don't see a reason why popup should be shown twice. – dotnetom 20 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.