Create and Edit in the same view

Hi,

I’m sarting with Ionic and i’m French, so, if i’m not clear, don’t hesitate to tell me !

I have a view which i can add an user in my app. Very basic, it contains only first name and last name inputs. By habit, when i do a simple view like this, i manage to use the same view for the edit purpose. To do that here, i try to add a var in my $scope, and, in my templates, with a ternary, i put what i want according to this variable. Problem ; i can’t access to this var…

The buttons add and edit are in the view “peoples” and the form view is named ‘addpeople’ , but the two views have the same controller.

Here is my code

When i add a user , i call the openadd function

$scope.openadd = function() {

$scope.formadduser = {
    first: '',
    last : ''
};
    $scope.test = 0;
$state.go('addpeople');

};

When i edit a user :

$scope.editadd = function() {
$scope.test = 1;

$state.go('addpeople');

};

And, in my view :

<ion-view view-title="{{test && 'class-when-true' || 'class-when-false' }}">

But the “test” value, when i print it, is blanck…I suppose this is because i change the view, but I’m still in the same controller, yet…

Can you tell me what is wrong, and if there is a cleaner way to reach my goal ?

Thanks

you have to instanciate your variable test outside your methods, otherwise, you can’t access it from two different functions I think.
just add $scope.test = 0 before your functions, and tell us what you have.

Hi,

I decided to change my way of doing and now, i juste create an object user which i bind with my form, but i have exactly the same problem, and when i did what you propose to me, the is no more effect…

Here is my entire code :

.controller(“Peoples”,[’$scope’, ‘$cordovaSQLite’, ‘$state’, ‘$ionicPopup’, function($scope, $cordovaSQLite, $state, $ionicPopup) {

$scope.user = {
    first : '',
    last : ''
};
//Every time the view is opened
$scope.$on('$ionicView.enter', function() {
    var query = "SELECT * FROM people";
    $cordovaSQLite.execute(db, query).then(function(res) {
        if(res.rows.length > 0) {
            $scope.people = [];
            if(res.rows.length > 0) {
                for(var i = 0; i < res.rows.length; i++) {
                    $scope.people.push({firstname: res.rows.item(i).firstname, lastname: res.rows.item(i).lastname, id : res.rows.item(i).id});
                }
            }
        }
    }, function (err) {
        console.error(err);
    });
});
$scope.openadd = function(num) {
    if (num == 0)
    {
        $scope.user = {
            first : 'fvdsvd',
            last : 'dfvdv'
        };          
           $state.go('addpeople');
    }
    else
    {
        $state.go('addpeople');
    }
};

And the view :

People, in which you can edit or create or edit a user

<ion-content class="padding">
    <ion-list ng-repeat="post in people">
        <ion-item href="#">
            <h2>{{ post.firstname }}  {{post.lastname}}</h2>
            <ion-option-button class="button-positive" ng-click="openadd(post.id)">Edit</ion-option-button>
            <ion-option-button class="button-assertive" ng-click="showConfirm(post.id)">Delete</ion-option-button>
        </ion-item>
    </ion-list>
</ion-content>
<div class="testHello">
    <a ng-click="openadd(0)" class="button button-icon ion-person-add icon-change">
    </a>
</div>

And the view with the form :

    <form class="list" novalidate>
        <label class="item item-input">
            <input type="text" placeholder="First Name" ng-model="user.first" required>
        </label>
        <div class="padding">
            <p ng-show="formAdduser.first.$error.required">First name is required</p>
        </div>
        <label class="item item-input">
            <input type="text" placeholder="Last Name" ng-model="user.last" required>
        </label>
        <h2>{{test}}</h2>
        <div class="padding">
            <p ng-show="formAdduser.last.$error.required">Last name is required</p>
        </div>
        <div class="padding">
        <button class="button button-block button-balanced"  ng-click="add()">
            Valider
        </button>
        </div>
    </form>
</ion-content>

I want that when i update the User object in the methode openadd(), i upadte also the view, but there is no effect. When i dit it on the controller but not in a methode, it works…

What is wrong ?

Nobody has an idea? I’m stuck on this problem for three days …

I see what is happening.

If i put

alert(JSON.stringify($scope.user));

juste after

$state.go(‘addpeople’);

User has good values. I put also the alert into the begginning of my controller, and user is reset. (In first, the alert of the function is good, next, the alert of the beginning of the controller appear and user is reset.)

It is as if the controller had returned to 0, then he is supposed be the same…