gpt4 book ai didi

javascript - 通过 Angular Directive(指令)访问 watch 内的变量

转载 作者:行者123 更新时间:2023-12-03 07:27:20 25 4
gpt4 key购买 nike

我是 Angular 新手,在 watch 方面遇到了一些问题。我正在观察 Controller 内的变量。一旦变量的值发生更改,我想将更改后的值发送到指令。

这是 watch 功能:

$scope.$watch("comboDetail", function(newValue, oldValue) {
$scope.overlayProductCard = $scope.comboDetail.collectionCd;
console.log("$scope.overlayProductCardWithinWatch", $scope.overlayProductCard);
}

我的指令:(为了阅读而剪掉)

return {
scope: {
productCard: '='
},
restrict: 'E',
templateUrl: appVersionFactory.getViewBaseUrl() + '/assets/partials/tv/tv-overlay-link.html',
replace: true,
link: function($scope, iElm, iAttrs, controller) {
console.log("$scope.productCard", $scope.productCard);

};

但是,$scope.productCard 返回未定义给我,而$scope.overlayProductCardWithinWatch 返回正确的数据。

我如何在指令中接收这些数据?

最佳答案

假设我有类似 <test param="controllerParam"></test> 的指令带模板<div>{{param}}</div> .如果我改变controllerParam在 Controller 中,我不需要观看/其他东西 - 指令将获得新的值(value)。如果我想在指令每次更改时记录新值 - 那么我需要在指令中进行监视。

链接函数每个元素执行一次,因此您只需记录变量值一次。 (第一个)

关于javascript - 通过 Angular Directive(指令)访问 watch 内的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35947381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com