0

I have a form in angular, that submits to an API, that returns a 201 status code, and the id and token of the object that was created. My idea is to open up a modal, with that token and show it to the user.

The value of $scope.object.token is updated, but I can't update that state in the view. I tried, $scope.$apply() I get an error $digest already in progress when calling $scope.$apply(). I also tried $timeout() but it doesn't update the view.

controller that handles that behavior is:

angular.module('myApp').controller('ObjectCtrl', ['$scope', 'user', 'object', '$routeParams', '$location', '$uibModal',
  function ($scope, user, object, $routeParams, $location, $uibModal, displayToken) { 

    $scope.object = {
      user_id: user.currentUser
    }

    $scope.create_object = function() {
      var promise = object.create($scope.object);
      promise.then(function(data){
      var token = data.data.token;

      var modalInstance = $uibModal.open({
        animation: $scope.animationsEnabled,
        templateUrl: '/app/views/modal_submit_token.html',
        controller: 'ObjectCtrl',
        resolve: {
          displayToken: function () {
            $scope.object.token = token;
          }
        }
      });
    });
  };
}]); 

And on my html,

<p><b>{{ object.token }}</b></p>
tvieira
  • 1,865
  • 3
  • 28
  • 45

1 Answers1

0

To pass the parameter you need to use resolve and inject the items in controller

$scope.Edit = function (Id) {
   var modalInstance = $modal.open({
      templateUrl: '/app/views/admin/addeditphone.html',
      controller: 'EditCtrl',
      resolve: {
         editId: function () {
           return Id;
         }
       }
    });
}

Now if you will use like this:

app.controller('EditCtrl', ['$scope', '$location'
       , function ($scope, $location, editId)

in this case editId will be undefined. You need to inject it, like this:

app.controller('EditCtrl', ['$scope', '$location', 'editId'
     , function ($scope, $location, editId)

Now it will work smooth, I face the same problem many time, once injected, everything start working!

Font: Pass parameter to modal

Community
  • 1
  • 1
Emir Marques
  • 2,603
  • 2
  • 16
  • 22