使用ng-repeat track的一次性绑定不能更新

时间:2016-04-26 16:56:30

标签: javascript angularjs

我试图通过使用"跟踪"来最小化我的AngularJS应用程序中的观察者数量。在我的ngRepeat中,以及使用一次性绑定。

e.g。

我的观点:

<div ng-repeat="item in items track by trackingId(item)">
  {{ ::item.updated_at }}  {{ ::item.id }}
</div>

我的trackingId和范围看起来像这样:

$scope.items = [
  { id: 1, updated_at: 'January 1, 2015' },
  { id: 2, updated_at: 'January 1, 2016' },
  ...
]

$scope.trackingId = function(item) {
  return item.id + '_' + item.updated_at;
}

现在,在我的应用中的某个地方,项目updated_at值会发生变化。例如,

$scope.updateItem = function(item) {
  item.updated_at = Date.now();
}

我希望该视图能够将此项目反映为&#34; new&#34; DOM中的项目由于该项目的trackingId值被更改,从而更新了一次性绑定值,但是没有发生这种情况。

我对一次性绑定+ trackBy的理解是不正确的?如何实现上述目标,如果我的trackingId发生变化,该项目将在视图上重新绘制?

谢谢,

2 个答案:

答案 0 :(得分:0)

当您更新附加到范围的项目时,您必须让角度知道绑定已更改,并且使用$ scope。$ apply()。

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Sleep, 1000 ;language selection and next.
Send, {tab}
Sleep, 500
Send, {tab}
Sleep, 500
Send, {tab}
Sleep, 500
Sleep, 2000
Send, {Enter}

Sleep, 1000 ;directory and installation.
Send, {tab}
Sleep, 500
Send, {tab}
Sleep, 500
Send, {tab}
Sleep, 500
Send, {tab}
Sleep, 500
Send, {tab}
Sleep, 5000
Send, {Enter}
Sleep, 500
Send, {tab}
Sleep, 5000 ;for installation wait time.
Send, {Enter}   ;finish.

Sleep, 7000
Run "myexecutable.exe"

Sleep, 4000 ;focus attempt 2.
Send, {control down}
MouseClick, Left, 300, 185,
Send, {Control up} ;for association OK.

答案 1 :(得分:0)

也许这不是个好主意

@property CGRect viewFrame;
@property CGRect tabbarFrame;

- (void)viewWillAppear:(BOOL)animated {
if (CGRectIsEmpty(self.viewFrame)) {
        self.viewFrame=self.view.frame;
        self.tabbarFrame=self.tabBarController.view.frame;
    }
}
- (void)viewDidAppear:(BOOL)animated {
 [self correctScreenSize];
}
-(void)correctScreenSize
{
    self.view.frame = self.viewFrame;
    [self.view layoutIfNeeded];
    self.tabBarController.view.frame  = self.tabbarFrame;
    [self.tabBarController.view layoutIfNeeded];

}

尝试没有这个。例如只有id