AngularJS Classic:我ng-click
在视图上更改我的模型没有更新。我以为$scope.$apply()
会进行更新,但是我无法正常工作。我想我没有$apply()
放在正确的地方或类似的地方。
示例: 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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句