gpt4 book ai didi

javascript - AngularJS $在范围内应用

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

是否有另一种方法可用于执行以下操作?这个 jQuery 函数在 Angular $scope 内部使用,它工作得很好,但是它会抛出 $apply 错误,本质上是因为它被视为 $apply 内的 $apply。然而,如果我删除 $scope.$apply() 行,它就会停止工作。

function myfunction(start, end) {
// Lots of jQuery code here - omitted from this example

// Update the scope
$scope.myf = "abc";
$scope.myt = "def";
$scope.$apply();
}

myfunction();

最佳答案

要么将代码包装在 $timeout 中,要么使用 $apply()。请注意,$timeout 在内部调用$apply()。 $apply 是专门为将外部更改(不在 Angular 循环中)与 Angular 重新同步而设计的。

如果您的函数在点击或事件时触发。您可以使用指令 ng-click 或 ng-[event] 将函数绑定(bind)到 Angular。

关于javascript - AngularJS $在范围内应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35844972/

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