kenza
kenza

Reputation: 195

how can i use display and use $scope in another controller in angular js

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;



        };

    })

Upvotes: 0

Views: 134

Answers (1)

dev
dev

Reputation: 313

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();
});

Upvotes: 1

Related Questions