从指令's使用$scope的独立作用域$父级不更新模板

updating the value of a parent variable from a directive's isolated scope using $scope.$parent do not update template

本文关键字:作用域 更新 独立 scope 指令 使用      更新时间:2023-09-26

我正在使用$scope从指令的独立作用域更新父变量的值$parent.parntvariable.field=新字段。

当我像console.log($scope.$parent.parentvariable.field)一样控制台时,该字段包含控制台中看到的新值。但是,应该更新的模板(因为它与字段绑定)没有得到更新。

使用$apply()作为$scope$家长$apply()引发$digest已在使用错误。

这里出了什么问题?

尝试做一些类似的事情:

$scope.$parent.group_levers.levers.forEach(function (lv,indexLever) {
    if (lv.RFLeverId == lever.RFLeverId) {
        lv.description.leverValue.forEach(function (leverVal, indexLeverVal) {
            if (leverVal.RFLeverValueId == splashResponseRaw.levers[0].description.leverValue[0].RFLeverValueId) {
                $scope.$parent.group_levers.levers[indexLever].description.leverValue[indexLeverVal] = splashResponse;
                //$scope.$parent.$apply();
            }
        })
    }
})
$timeout(function () {
    $scope.$parent.$apply();
}, 10)

试试这个。在一小部分毫秒后运行apply。

$timeout(function(){
  scope.$apply();
},100)