服务变量未在AngularJS中更新
我有一个变量workingIdx,需要更新为它所单击的div的id。它会更新,但当我再次查询它时,它不会显示新值 我的服务:服务变量未在AngularJS中更新,angularjs,Angularjs,我有一个变量workingIdx,需要更新为它所单击的div的id。它会更新,但当我再次查询它时,它不会显示新值 我的服务: writingPage.service('Lctr', function() { this.workingIdx = 0; return { getWorkingIdx: this.workingIdx, setWorkingIdx: function(i) { this.workingIdx = i;
writingPage.service('Lctr', function() {
this.workingIdx = 0;
return {
getWorkingIdx: this.workingIdx,
setWorkingIdx: function(i) {
this.workingIdx = i;
console.log("wIdx="+this.workingIdx);
}
};
});
我正在进行更新和检查的控制器
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;
}
}
});
}]);
}
writingPage.controller('DataController',['SnippetPojo','$scope','$timeout','Lctr',函数(SnippetPojo,$scope,$timeout,Lctr){
$scope.currendisprine='';
$scope.myId=-1;
$scope.localCursor=0;
$scope.hideOrShow=函数(id,currentDivCursor){
如果(id)
单击DIV时我看到设置了wIdx=1,然后在DIV中按enter键时显示输入的ID=0 wIdxNEW=0,尝试将其更改为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);
}
};
});
此
在函数中获取一个新值您好,尝试了此操作,但没有解决问题。在控制台中仍然可以看到相同的值。服务对象的行为是否与向每个控制器提供相同信息的单例不一样?它确实如此,但self.workingIdx=0;
会在您每次注入时重置该值,就像s一样开始刷新谢谢!我试图将getWorkingIdx转换为函数,但现在该函数被分配给变量,并且没有返回workingIdx的值。我做错了什么?getWorkingIdx:function(){if(self.workingIdx<0)self.workingIdx=0;返回self.workingIdx;},
您还应该检查是否存在未定义的
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);
}
};
});