0

I'm now developing website and there has edit note field features in ng-repeat. To edit note field, user need to click link to display form first then key-in into it and then save it as follow. Problem is i cannot hide that input after successfully saved. Coding is as follow.

index.jade

tr(data-ng-repeat="application in job.applications")
    td.notes
        div.bold #{getMessage('Notes:')}
        div.normal
            div(ng-hide='showDetails')
                {{application.note}}
                .br
                a.admin_edit_gray(href='#', ng-click="showDetails = ! showDetails") Edit Note
            div(ng-show='showDetails')
                textarea.form-control.small-text-font(ng-model='editableTitle', ng-show='showDetails', maxlength="100", ng-trim="false")
                div.editable
                    div(ng-if="editableTitle.length == 100") 
                        | #{getMessage('max 100 symbols.')}
                a.small-text-editButton(href='#', ng-click='save(application, editableTitle, application.id)') Save 
                | | 
                a.small-text-cancelButton(href='#', ng-click="showDetails = ! showDetails") close

controller.js

$scope.showDetails = false;        
$scope.noteFormData = {};
$scope.save = function(application, editableTitle, appId) {
    $scope.noteFormData = {
        appId: appId,
        note: editableTitle
    };
    mytestService.writeNote($scope.noteFormData).then(
        function (notemessage) {
            application.note = notemessage;
            alert('Note is successfully saved.');
            $scope.showDetails = false;
        }
    );
};

I've tried to hide form as $scope.showDetails = false; after successfully saved. But it does not work at all. Please help me how to solve that issue.

4
  • Try wrapping $scope.showDetails = false in a $timeout, could be a digest issue. Commented Mar 25, 2016 at 5:32
  • Is writeNote making some AJAX call to post the updates to your server using $http ? Commented Mar 25, 2016 at 5:50
  • @Arkantos yap, sure. it's async call. Commented Mar 25, 2016 at 6:38
  • @Layoric it does not work in $timeout. Commented Mar 25, 2016 at 6:51

2 Answers 2

1

You are creating showDetails inside the $scope of the ngRepeat. Each iteration of the loop creates a new child $scope of the controller's $scope.

In this way, just set $scope.showDetails from the controller will not work.

In order to fix that you need to get the reference to the object that is being iterated and set the show details:

Instead of:

ng-click="showDetails=!showDetails"

Use:

ng-click="application.showDetails=!application.showDetails"

After that, when submiting, you can choose which one you would like to show or hide by using the correct reference or by iterating over all itens of the array and setting showDetails to false.

Instead of:

$scope.showDetails = false;

Use:

application.showDetails = false;
Sign up to request clarification or add additional context in comments.

1 Comment

Good explanation and example. Appreciate it.
0

set a variable in controller and set its value false .After your save() function is executed successfully set that variable to true. And in the view page put an condition of ng-show on tr if that value that is true.

1 Comment

what you said is same as what I did.

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.