hussain
hussain

Reputation: 7103

how to use get and set using angularjs factory?

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>

Upvotes: 0

Views: 41

Answers (1)

Reza
Reza

Reputation: 867

$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);

} }

Upvotes: 1

Related Questions