0

I've checked various threads and tutorials, simply trying to add an item to an ionic list by clicking on a button. However, I'm not able to make it. I'm pretty sure that I'm the problem.. :)

Please find my code below. Assigning a date to newDate works fine, and the array groups works fine to create a list (via ng-repeat). The problem occurs when I'm trying to run the function addEntry via a button, no item is added to the list and I'm receiving "Error: groups is not defined" in the console log.

I hope that it's a simple declaration problem which can be solved by a one sentence answer... thanks in advance!!!

HTML Button:

<button class="button button-block button-balanced" ng-click="doRefresh()" ui-sref="menu.tagebucheintrag">
Send
</button>

In controller:

$scope.groups = [{name:'25.11.2016'},{name:'26.11.2016'},{name:'27.11.2016'},{name:'28.11.2016'}];

$scope.addEntry = function () {     
    console.log('Button pressed');
    var newDate = FromDate
    console.log(newDate);
    $scope.groups.push({ name: newDate });
    console.log(groups[0])
};
3
  • What is fromDate? Commented Jan 8, 2017 at 11:39
  • fromDate is the current date as string. But this is fine, console.log(newDate); provides me "2017-01-08". Commented Jan 8, 2017 at 11:43
  • How did you get that? did you define it somewhere else in the code? Commented Jan 8, 2017 at 11:50

1 Answer 1

1

On clicking button you are calling doRefresh() but there is no refresh function. You have to change ng-click="doRefresh()" to ng-click="addEntry()" to push the date if fromDate is works.

<button class="button button-block button-balanced" ng-click="addEntry()" ui-sref="menu.tagebucheintrag">
Send
</button>

Otherwise do this

$scope.groups = [{name:'25.11.2016'},{name:'26.11.2016'},{name:'27.11.2016'},{name:'28.11.2016'}];
$scope.addEntry= function(){
     $scope.newDate = new Date().getDate() + '.' + (new Date().getMonth()+1) + '.' + new Date().getFullYear();
     $scope.groups.push({ name: newDate });
}
Sign up to request clarification or add additional context in comments.

4 Comments

Oh, classy. I renamed it half way through. Thanks for the hint, but the problem remains...
Yes, I'm still receiving the error that groups is not defined.. do I have to declare groups different in case I want to use it in a function?
I thought you were added $scope.groups in your code. Now, see the updated answe
Yes, sure, as it is shown in the initial post. Thanks for the on going support :D I've added $scope.groups to declare it, but I was wondering whether I have to mark it as global or what not to use it in the function addEntry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.