0

I am setting and getting values using angularjs factory ,These are live messages that are coming into mainCtrl I want to take data that is currently in $scope.event when user search log by searchLogs() I have data populated into modal window that is working but problem is i am keep getting the data from mainCtrl how to stop binding once i have data in modal window at some point i dont want to get data so user can make search that is available in the $scope ?

factory.js

angular.module('App').factory('searchFactory', function ($http) {
    'use strict';
     var logs;
    return {
        getDitLogs : function () {
            return logs;
        },

        setDitLogs : function (data) {
            logs  = data;
        }
    }

});

mainCtrl.js

  $scope.searchLogs = function () {
    $scope.modalInstance = $uibModal.open({
        templateUrl: '/view/modals/searchModal.html',
        controller:'SearchController'
    });
    searchFactory.setDitLogs($scope.event);
}

childCtrl.js

$scope.event = searchFactory.getDitLogs();
    console.log(searchFactory.getDitLogs());

main.html

 <div class="col-md-12">
            <ul style="list-style: none;">
                <li ng-repeat="message in event track by $index"><span><strong>Log:</strong></span><span>{{message}}</span></li>
            </ul>
        </div>

1 Answer 1

1

$scope.modalInstance has a property called opened which is a promise. one way is to wait for that promise to be resolved, and then set a flag so that searchFactory.setDitLogs() won't be called:

$scope.searchLogs = function () {
if(!$scope.doNotSetDit){
   $scope.modalInstance = $uibModal.open({
       templateUrl: '/view/modals/searchModal.html',
       controller:'SearchController'
   });
   $scope.modelInstance.opened.then(function(){$scope.doNotSetDit =true});
   searchFactory.setDitLogs($scope.event);

} }

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

1 Comment

Thanks this solution worked for me , another solution i found is angular.copy(data) will create new instance for you.

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.