3
AngularJS Classic:Im更改我的模型ng-click
视图没有更新。我认为简单的$scope.$apply()
会更新,但我没有得到它的工作。我想我不是在正确的地方设置$apply()
或类似的东西。AngularJS - 模型更改没有更新视图
实施例:Plunker
对象的名称上按键来改变。
AngularJS Classic:Im更改我的模型ng-click
视图没有更新。我认为简单的$scope.$apply()
会更新,但我没有得到它的工作。我想我不是在正确的地方设置$apply()
或类似的东西。AngularJS - 模型更改没有更新视图
实施例:Plunker
对象的名称上按键来改变。
点击后,只需更新explorerObject
一个更多的时间,因为它仍然是以前的对象指向:
$scope.click = function() {
ExplorerService.setExplorerObject({
selected : {
name: 'Defined Now !',
id: 'id',
timestamp: 'timestamp'
},
templateURL: 'views/beispiel.html'
});
$scope.explorerObject = ExplorerService.getExplorerObject(); // <--- here
}
工作:http://plnkr.co/edit/UWE7o3mtAzY3xzYRWfkf?p=preview
问题后”编辑:
你可以在您的第二个控制器中使用$watch
:
app.controller('SecondCtrl', function($scope, ExplorerService) {
$scope.$watch(function(){
return ExplorerService.getExplorerObject();
},function(n,o){
$scope.explorerObject = ExplorerService.getExplorerObject();
},true)
});
工作:http://plnkr.co/edit/8mZO5kZmTrqwHKnxtBSd?p=preview
或者使用$broadcast
的方法,如:
app.controller('SecondCtrl', function($scope, ExplorerService) {
$scope.explorerObject = ExplorerService.getExplorerObject();
$scope.$on("EXPLORER_OBJECT_CHANGED" ,function(event,obj){
$scope.explorerObject = obj;
});
});
而在你的服务添加:$rootScope.$broadcast("EXPLORER_OBJECT_CHANGED", explorerObject);
我很抱歉,我的榜样并不完全代表我的问题。我现在更新了它。谢谢您的回答。 [Plunker](http://plnkr.co/edit/8rREFdLKvznaNLlaj6Hm) – strangfeld
@strangfeld好吧,看看我的更新 – Cherniv
哪种方法更高性能/更便宜? – strangfeld