1

I am new to AngularJS. So, I am creating an app where I have a list of users that when a button is clicked, it need to remove that user from its list. The users are getting from a backend API. However, for some reason, the user is not being removed from the list when the "Confirm button" is clicked.

Here is my code

about.html

    <div ng-controller="MyCtrl">
<div ng-repeat="person in userInfo.users | filter : {id: userId}">

<a class="back" href="#/user">Back</a>

  <button type="button" class="edit" ng-show="inactive" ng-click="inactive = !inactive">
    Edit
  </button>

  <button type="submit" class="submit" ng-show="!inactive" ng-click="inactive = !inactive">Save</button>

  <a class="delete" ng-click="confirmClick(); confirmedAction($index);" confirm-click>Confirm</a>



  <div class="people-view">

    <h2 class="name">{{person.firstName}}</h2>

    <h2 class="name">{{person.lastName}}</h2>

    <span class="title">{{person.email}}</span>

    <span class="date">{{person.website}} </span>


</div>

  <div class="list-view">

    <form>

      <fieldset ng-disabled="inactive">

        <legend>Main Users</legend>

        <b>First Name:</b>

        <input type="text" ng-model="person.firstName">
        <br>

        <b>Last Name:</b>

        <input type="text" ng-model="person.lastName">
        <br>

        <b>Email:</b>

        <input type="email" ng-model="person.email">



        <br>

      </fieldset>

    </form>

  </div>
</div>
</div>

App.js

     var app = angular.module("MainPortal", ['ngRoute',  'ui.bootstrap' ]);



      app.controller('MyCtrl', function($scope) {


       $scope.inactive = true;

    $scope.confirmedAction = function(index) {

    $scope.userInfo.users.splice(index.id, 1);

    location.href = '#/user';





};

});


app.directive('confirmClick', ['$q', 'dialogModal', function($q, dialogModal) {
    return {
        link: function (scope, element, attrs) {

            var ngClick = attrs.ngClick.replace('confirmClick()', 'true')
                .replace('confirmClick(', 'confirmClick(true,');


            scope.confirmClick = function(msg) {

                if (msg===true) {
                    return true;
                }
          confirm?')

                msg = msg || attrs.confirmClick || 'Are you sure you want to confirm?';

                dialogModal(msg).result.then(function() {
                    scope.$eval(ngClick);
                });

                return false;
            };
        }
    }
}])


    .service('dialogModal', ['$modal', function($modal) {
    return function (message, title, okButton, cancelButton) {

        cancelButton = cancelButton===false ? false : (cancelButton || 
         'No');
        okButton = okButton ===false ? false : (okButton || 'Yes');


        var ModalInstanceCtrl = function ($scope, $modalInstance, settings) 
          {

            angular.extend($scope, settings);

            $scope.ok = function () {
                $modalInstance.close(true);
            };

            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
             };


            var modalInstance = $modal.open({
            template: '<div class="dialog-modal"> \
              <div class="modal-header" ng-show="modalTitle"> \
                  <h3 class="modal-title">{{modalTitle}}</h3> \
              </div> \
              <div class="modal-body">{{modalBody}}</div> \
              <div class="modal-footer"> \
                  <button class="btn btn-primary" ng-click="ok()" ng-
            show="okButton">{{okButton}}</button> \
                  <button class="btn btn-warning" ng-click="cancel()" ng-
               show="cancelButton">{{cancelButton}}</button> \
              </div> \
          </div>',
            controller: ModalInstanceCtrl,
            resolve: {
                settings: function() {
                    return {
                        modalTitle: title,
                        modalBody: message,
                        okButton: okButton,
                        cancelButton: cancelButton
                      };
                   }
                }
            });

          return modalInstance;
       }
     }])


    app.config(function ($routeProvider) {
    $routeProvider
    .when("/user", {
      controller: "HomeController",
    templateUrl: "partials/home.html"
    })
     .when("/user/:id", {
    controller: "UserController",
    templateUrl: "partials/about.html"

   })
     .otherwise({
    redirectTo: '/user'

    });

   });

HomeController

    //var isConfirmed = false;
app.controller('HomeController', function($scope, people, $http) {
   // if (!isConfirmed) {
        people.getUserInfo().then(function (response) {

      $scope.userInfo = response.data;



        }, function (error) {
            console.log(error)
        });
   // }
}); 

UserController

app.controller('UserController', ['$scope', 'people', '$routeParams',
    function ($scope, people, $routeParams) {

        $scope.userId = $routeParams.id;
        people.getUserInfo().then(function (response) {
            $scope.userInfo = response.data;
        });
    }]); 

1 Answer 1

2

This line $scope.userInfo.users.splice(index.id, 1); will only remove object from an array.

With this line location.href = '#/user'; you will lose $scope and automatically $scope.userInfo.users, as you never remove object. Also line location.href = '#/user'; will call HomeController and there you have this call

people.getUserInfo().then(function (response) {
  $scope.userInfo = response.data;
    }, function (error) {
        console.log(error)
    });

which will retrive list of objects with object you previous remove (because you didn't send request to server to delete object).

In order to delete object, you have to execute call to server, some $http.delete method.

Sign up to request clarification or add additional context in comments.

5 Comments

can you please show me a snippet of code prior to my code above, so I can understand how this method works.
How about to remove it only locally?
What is the purpose to remove it only locally?
Well, now I just don't want to delete the user. I just want to set that user to true when the confirm button is clicked.
Just remove this line location.href = '#/user'; from confirmedAction function. Function confirmedAction should look like: $scope.confirmedAction = function(index) { $scope.userInfo.users.splice(index.id, 1); }; This will remove object from list and stay on that page.

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.