$scope变量发生更改时未触发Angular$watch
Angular $watch not triggered on change in $scope variable
所以我有一个例程,需要使用在应用程序初始化时检索到的用户数据来运行。
我的主页控制器检查存储中是否有令牌(登录用户),然后如果$scope.currentUser已经存在,则运行例程,或者监视AJAX调用何时返回该数据。
但是$watch回调在初始化之后永远不会被触发。
这是进料控制器代码
if (sessionStorage.getItem('token'))
{
if ($scope.currentUser) {getItems()}
else {$scope.$watch('currentUser', popFeed()) }
}
function delayForUser()
{
if ($scope.currentUser) {popFeed()}
}
应用程序控制器代码
if (sessionStorage.getItem('token') && sessionStorage != 'null')
{
UserSvc.getUser()
.then(function (response)
{
$scope.currentUser = response.data
})
假设您可以从两个控制器中看到currentUser
(您提到它们是不同的),您需要在$watch
上传递一个函数,而不是立即执行它(使用(),这是该函数的结果):
$scope.$watch('currentUser', function(newValue) {
popFeed();
});
相关文章:
- $scope变量发生更改时未触发Angular$watch
- Angular JS$编译服务导致$watch内存泄漏
- nodejs中是否有类似angular$watch的东西
- Angular$watch未更新绑定
- watch()函数无法在json调用上使用angular.js
- 在承诺函数从 pouchdb 填充数据之前触发 Ionic/Angular $watch的问题
- 对 angular.extend 的调用不会导致 $scope.$watch proc
- Angular.js deep watch Coffeescript class instance
- 如何在 Angular 1.5 组件中等待绑定(没有 $scope.$watch)
- Angular 不会保存 $scope.$watch 顺序
- 如何在核心javascript中实现angular$watch函数
- 多维数组上的Angular js$watch
- Issue With Angular $scope.Watch
- Angular$watch旧值在用于观察li时总是等于新值
- Angular$watch|从函数返回项
- angular$watch-can'当集合是相同的变量时,不要让手表开火
- Angular-$watch检查服务状态
- 过滤集合上的Angular watch侦听器在错误的时间调用
- Angular Directive$watch没有响应
- 获取Angular.JS中watch事件的DOM元素