似乎当我为服务/工厂中的变量分配新值时,它会破坏绑定并且控制器停止更新值,除非我$scope.verify
在控制器中调用该函数,该函数仅将服务对象打印到控制台,然后绑定更新一次。我是否在skypeClient
工厂中分配了错误的值?
property.changed(function (_status) {
state = _status;
console.log("New State" + state);
});
前任。我执行$scope.signIn()
并且绑定更新到,signingIn
但是当值更改为SignedIn
(使用控制台验证)时,控制器不会更新到,SignedIn
除非我从那里开始执行$scope.verify()
每个更改。skypeClient.state
请参见下面的代码:
控制器
controller('loginCntrl', function ($scope,skypeClient) {
$scope.skypeClient = skypeClient;
$scope.signIn = function () {$scope.skypeClient.signIn($scope.user, $scope.password)}
$scope.signOut = function(){$scope.skypeClient.signOut()}
$scope.verify = function () {
console.log(skypeClient);
console.log($scope.skypeClient);
}
});
服务
.factory('skypeClient', function () {
//Service Properties
var client = new Skype.Web.Model.Application;
var state = 'SignedOut';
var property = property = client.signInManager.state;
//Initialize Listeners
var init = function () {
client.signInManager.state.when('SignedIn', function () {
console.log('Signin:' + state); // This outputs the correct value
});
property.changed(function (_status) {
state = _status; //<--WHERE VALUE IS UPDATED
console.log("New State" + state);
});
}
//Signin Function
var signIn = function (username, password) {
client.signInManager.signIn({
username: username,
password: password
}).then(function () {console.log('LoggedIn');});
}
var signOut = function () {
client.signInManager.signOut()
.then(function () {
this.isSignedIn = false;
}, function (error) {
this.erros.push(error);
this.errorCount++;
});
}
init();
return {
signIn: signIn,
signOut, signOut,
state: function(){return state}
}
});
HTML
(Current Signin State: {{skypeClient.state()}} )