私は現在のユーザーのイメージを表示する "nav"コントローラを備えたnavbarを持っています。しかし、ユーザーがプロフィール写真を更新した場合、ナビ画像を変更することができません。ナビゲーションコントローラ内の
'use strict';
angular.module('clientApp').factory('Account', function ($http, toastr) {
var currentUser = null;
function getUser(callback, user) {
console.log('get user user ', user)
// if the user has already been retrieved then do not do it again, just return the retrieved instance
if (currentUser !== null && currentUser !== user) {
callback(currentUser);
}
if (currentUser === null) {
// retrieve the currentUser and set it as a property on the service
$http.get('/api/me').then(function (res) {
// set the result to a field on the service
currentUser = res.data;
// call the callback with the retrieved user
callback(currentUser);
});
}
}
//updates the currentUser variable
function updateProfile(profileData, callback) {
console.log('profile data ', profileData);
$http.put('/api/me', profileData).then(function (res) {
currentUser = res.data;
callback(currentUser);
}).catch(function (response) {
if (response.data.message.length) {
for (var i = 0; i < response.data.message.length; i++) {
toastr.error(response.data.message[i]);
}
}
});
}
return {
getUser: getUser,
updateProfile: updateProfile
};
});
ウォッチャー::私は明らかにここで何かが混乱してい
$scope.user = {};
//watches for currentUser changes in account service
$scope.$watch(Account.getUser(function(user){}, $scope.user), function (newValue, oldValue) {
console.log('user update watch', newValue)
$scope.user = newValue;
});
は、ここに私のサービスです。私は$ watchの情報を見ていますが、これは私が期待したようにうまくいきません。おそらく、Account.getUserは$ watchの値を比較するために何も返さないので、代わりにコールバックを使用するからです。
誰かが間違っているところを指摘できたら、
@NoobSterところで、単にコールバックを使用していて、単に約束を利用しているのではないのですか? –