3

似乎当我为服务/工厂中的变量分配新值时,它会破坏绑定并且控制器停止更新值,除非我$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()}} )
4

1 回答 1

0

让我试试这个。

如果可以,请尝试将状态设置为需要在绑定中显示的范围内的变量。

控制器

controller('loginCntrl', function ($scope,skypeClient) {
    ...
    $scope.state = skypeClient.state();
    ...
});

HTML

(Current Signin State: {{ state }} )

现在,在您的控制器中,您可以从 skypeClient 将 $watch 添加到状态变量。如果我是正确的,事情应该会更成功地更新。

控制器(再次)

controller('loginCntrl', function ($scope,skypeClient) {
    ...
    $scope.state = skypeClient.state();
    ...
    // Add this $watch

    $scope.$watch(skypeClient.state, function (newValue, oldValue) {
        if (newValue !== oldValue) {
            $scope.state = newValue;
        }
    });
});
于 2015-08-17T21:04:46.397 回答