1

i have a variable workingIdx that needs to be updated to the id the div it is clicked on. It updates but when i query it again, it does not show new value.

My service:

writingPage.service('Lctr', function() {

this.workingIdx = 0;
    return {
        getWorkingIdx: this.workingIdx,
        setWorkingIdx: function(i) {
            this.workingIdx = i;
            console.log("wIdx="+this.workingIdx);
        }
    };
});

My Controller where i am doing the updating and checking...

writingPage.controller('DataController' , ['SnippetPojo','$scope', '$timeout', 'Lctr', function(SnippetPojo, $scope, $timeout, Lctr) {

$scope.currDispLine = '';
$scope.myId = -1;
    $scope.localCursor = 0;

$scope.hideOrShow = function(id, currentDivCursor) {
    if(id <= currentDivCursor)
    {
        console.log("wIdx="+Lctr.getWorkingIdx+ " id="+id);
        return false;
    }
    else
    {
        return true;
    }

}

$scope.click = function(id) {
            $scope.myId = id;
            Lctr.setWorkingIdx($scope.myId);
    console.log('MyId='+$scope.myId);
}

$('div').on( "keypress", function( event ) {
    console.log("Event="+event.keyCode + " : "+event.which);
    code = (event.keyCode ? event.keyCode : event.which);
    if ( (code == 37 || code == 39) && event.shiftKey == true) 
    {    
            $timeout(function() {console.log('Selection set= ' +window.getSelection()); return false;}, 2200);

    }
    if ( code == 13) {
                console.log("ENTERed ID="+$scope.myId + " wIdxOLD = "+ Lctr.getWorkingIdx);
        if(Lctr.getWorkingIdx == $scope.myId) {

            Lctr.setWorkingIdx(Number(Lctr.getWorkingIdx) +1);
            localCursor = (Number(Lctr.getWorkingIdx) +1);
            console.log("ENTERed ID="+$scope.myId + " wIdxNEW = "+ Lctr.getWorkingIdx);

            $('#'+$scope.myId).blur();
            $('#'+(Number(Lctr.getWorkingIdx) +1)).mousedown();

            return false;
        }
    }
});
}]);
}

Finally the HTML

<div id='repeater' data-ng-controller='RepeatController'>
        <!--<data-ng-div-line data-ng-repeat="div in divs track by $index" id="{{$index}}"></data-ng-div-line>-->
        <div data-ng-repeat="div in divs track by $index" id="{{div}}" data-ng-controller='DataController' data-ng-click='click(div)' contenteditable="true" class="writediv" data-ng-model="localCursor" data-ng-mouseover="setId(div)" data-ng-show="!hideOrShow(div, localCursor)" data-ng-hide="hideOrShow(div, localCursor)"></div>
</div>  

I see wIdx=1 set when i click the DIV and then the ENTERed ID=0 wIdxNEW = 0 displayed when i press enter in the DIV,

1 Answer 1

1

try chaging this to self...

writingPage.service('Lctr', function() {

var self = this;

self.workingIdx = 0;
    return {
        getWorkingIdx: self.workingIdx,
        setWorkingIdx: function(i) {
            self.workingIdx = i;
            console.log("wIdx="+self.workingIdx);
        }
    };
});

this gets a new value inside a function

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

8 Comments

Hi, tried that but it did not solve the problem. still seeing same values in console.
Does the service object not behave as a singleton giving same information to each of the controllers?
it does but self.workingIdx = 0; would reset the value whenever you inject, so as good as starting fresh
Thanks! i tried to convert getWorkingIdx to a function but now the function is getting assigned to the variable and not returning the value of workingIdx. What am i doing wrong? getWorkingIdx: function() { if(self.workingIdx < 0) self.workingIdx = 0; return self.workingIdx; },
You should also check for undefined
|

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.