0

I have a $scope.selected ( a modal that return a value selected ) to be created in ModalInstanceCtrl controller and used in displayValue controller,How Can I spent the first controller value at the second,

app
    .controller('DemoCtrl', function ($scope, $modal) {

        console.log("in angular");

        $scope.selected = null;

        $scope.open = function() {

            var modalInstance = $modal.open({
                templateUrl: 'myModalContent.html',
                controller: 'ModalInstanceCtrl',
                size: 'lg'
            });

            modalInstance.result.then(function (selectedItem) {
                $scope.selected = selectedItem;

            });

        };


    })

.controller('ModalInstanceCtrl', function ($scope, $modalInstance) {

        $scope.setSelectedSegment = function (value) {

            $scope.selected = value;
            $modalInstance.close($scope.selected);


        };

    })

 .controller('displaySelected', function ($scope) {

       // get selected from ModalInstanceCtrl controller
            $scope.displayValue= $scope.selected;



        };

    })

1 Answer 1

1

You need to create angularjs service for this purpose and then include the service where you need to access its value.

var app = angular.module("MyApp", []);

app.factory("UserService", function() {
  var users = ["Peter", "Daniel", "Nina"];

  return {
    all: function() {
      return users;
    },
    first: function() {
      return users[0];
    }
  };
});

app.controller("MyCtrl", function($scope, UserService) {
  $scope.users = UserService.all();
});

app.controller("AnotherCtrl", function($scope, UserService) {
  $scope.firstUser = UserService.first();
});
Sign up to request clarification or add additional context in comments.

Comments

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.