简体   繁体   English

AngularJS重新初始化对象

[英]AngularJS re-initialize an object

I have an if condition in my Javascript as follows: 我的Javascript中有一个if条件,如下所示:

if($scope.cal){
     delete $scope.cal;
     $scope.cal = new CalHeatMap();
     var datedata = $scope.attendance.dates;

}else{
     $scope.cal = new CalHeatMap();
     var datedata = $scope.attendance.dates;
}

After referring a few posts, i realized we cannot completely delete an object. 引用几篇文章后,我意识到我们无法完全删除对象。 In my scenario, i need to initialize a new heatmap and replace the existing one. 在我的场景中,我需要初始化一个新的热图并替换现有的热图。 How do i get around in this situation? 在这种情况下我该如何解决?

Also tried: 还尝试了:

if($scope.cal){
     $scope.cal = {};
     $scope.cal = new CalHeatMap();...

You should not need to delete or blank an existing object on the $scope. 您无需删除或清空$ scope上的现有对象。 Just regular assignment should work whether a property is already defined or not. 无论属性是否已定义,仅常规分配都应该起作用。

$scope.cal = new CalHeatMap();

From what I understand of $scope, child scopes will not see the change however. 根据我对$ scope的了解,子作用域不会看到更改。 To work around that you would need to update an object on the $scope instead of replacing it. 要解决此问题,您需要更新$ scope上的对象而不是替换它。 Something like: 就像是:

$scope.vm.cal = new CalHeatMap();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM