AngularJS重新初始化一个对象

时间:2015-05-07 16:32:51

标签: javascript angularjs

我的Javascript中有if条件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/matcher_background"
    android:orientation="vertical">

    <com.***.ui.customviews.EditableTextView
        android:id="@+id/editable_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

在介绍几篇帖子后,我意识到我们无法完全删除一个对象。在我的场景中,我需要初始化一个新的热图并替换现有的热图。我如何在这种情况下解决?

也尝试过:

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;
}

1 个答案:

答案 0 :(得分:0)

您不需要删除或清空$ scope上的现有对象。无论是否已经定义了属性,只需定期分配即可。

$scope.cal = new CalHeatMap();

根据我对$ scope的理解,子范围不会看到更改。要解决这个问题,您需要更新$ scope上的对象而不是替换它。类似的东西:

$scope.vm.cal = new CalHeatMap();
相关问题