0

I have a case where I need to ask for confirmation on deleting data in Angular, I did it with this (along with Angular UI Bootstrap):

    $scope.delete = (id) ->
        $scope.setId = id //here the $scope.setId is 543
        $modal.open
            templateUrl: 'deleteModalContent.html'
            controller: modalInstanceController

    modalInstanceController = ['$scope', '$modalInstance', ($scope, $modalInstance) ->
        $scope.delete_ok = ->
            console.log $scope.setId //here the value becomes undefined

        $scope.delete_cancel = ->
            $modalInstance.dismiss 'cancel'
    ]

As I have stated my problem in the code (on the comment), the id becomes undefined in different controller, without $rootscope, how can I get around this?

1 Answer 1

2

Check out this thread What's the correct way to communicate between controllers in AngularJS?

You have to use the root scope to share the values, or a service.

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

4 Comments

And using the service is preferred solution for most cases.
I understand rootscope, but how does a service can help in this case?
A service is a singleton in Angular. If you use a service in your delete directive and also in the controller, you can expose a get and set for your setId in the service. However in your case I would recommend to do $rootScope.$emit('deleteEventName', setId) and listen to this in your controller with $scope.$on('deleteEventName', function (setId) { .. }
@Stefan: ah i see, still using $rootScope, I thought It will be a solution where I don't have to use that. Thanks for the fiddle.

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.