我发现自从以角度构建应用程序后,我需要手动将页面更新到我的范围.
我知道这样做的唯一方法是$apply()从我的控制器和指令的范围调用.这个问题是它不断向控制台抛出一个错误:
错误:$ digest已在进行中
有谁知道如何避免这个错误或以不同的方式实现相同的事情?
堆栈跟踪:
Error: $apply already in progress
at Error (<anonymous>)
at beginPhase (file:///android_asset/www/built.min.js:7:22740)
at Object.Scope.$apply (file:///android_asset/www/built.min.js:7:25967)
at navigator.geolocation.getCurrentPosition.that (file:///android_asset/www/built.min.js:13:8670)
at Object.geolocation.getCurrentPosition (file:///android_asset/www/plugins/org.apache.cordova.core.geolocation/www/geolocation.js:122:13)
at Object.getCurrentPosition (file:///android_asset/www/built.min.js:13:8589)
at Object.getCurrentPosition (file:///android_asset/www/built.min.js:13:8277)
at Object.getCurrentCity (file:///android_asset/www/built.min.js:13:8941)
at Object.$scope.locateDevice (file:///android_asset/www/built.min.js:13:10480)
at file:///android_asset/www/built.min.js:7:12292:7
Run Code Online (Sandbox Code Playgroud)
请参阅此代码http://pastebin.com/B9V6yvFu
getCurrentPosition: cordovaReady(function (onSuccess, onError, options) {
navigator.geolocation.getCurrentPosition(function () {
var that = this,
args = arguments;
if (onSuccess) {
$rootScope.$apply(function () {
onSuccess.apply(that, args);
});
}
}, function () {
var that = this,
args = arguments;
if (onError) {
$rootScope.$apply(function () { …Run Code Online (Sandbox Code Playgroud) 我的页面上有一个按钮,页面上有一个id ="viewQuestions":
<body ng-keydown="key($event);">
Run Code Online (Sandbox Code Playgroud)
我的代码正在检查这样的关键命中:
$scope.key = function ($event) {
$scope.$broadcast('key', $event.keyCode)
}
Run Code Online (Sandbox Code Playgroud)
并在控制器中:
$scope.$on('key', function (e, key) {
if (key == 13) {
if (ts.test.current && ts.test.userTestId) {
document.getElementById("viewQuestions").click();
}
}
});
Run Code Online (Sandbox Code Playgroud)
当我运行代码并单击ENTER然后我收到此错误:
Error: [$rootScope:inprog] $apply already in progress
http://errors.angularjs.org/1.3.7/$rootScope/inprog?p0=%24apply
at REGEX_STRING_REGEXP (http://localhost:2757/lib/angular/angular.js:63:12)
at beginPhase (http://localhost:2757/lib/angular/angular.js:14735:15)
at Scope.$get.Scope.$apply (http://localhost:2757/lib/angular/angular.js:14479:11)
at HTMLButtonElement.<anonymous> (http://localhost:2757/lib/angular/angular.js:22945:23)
at HTMLButtonElement.eventHandler (http://localhost:2757/lib/angular/angular.js:3009:21)
at http://localhost:2757/app/tests/controllers/TestsController.js:24:62
at Scope.$get.Scope.$broadcast (http://localhost:2757/lib/angular/angular.js:14700:28)
at Scope.AppController.$scope.key (http://localhost:2757/app/appController.js:30:20)
at $parseFunctionCall (http://localhost:2757/lib/angular/angular.js:12330:18)
at ngEventDirectives.(anonymous function).compile.element.on.callback (http://localhost:2757/lib/angular/angular.js:22940:17)
Run Code Online (Sandbox Code Playgroud)
任何人都可以给我这方面的建议.我只想在按下ENTER键时模拟按钮的单击.我确实尝试过使用表单的解决方案,但它不符合我相当复杂的需求.
编辑:似乎这可能与Chrome v43有关,我降级到v42,一切正常.
编辑:我已经在Angular的Github回购中提交了一个问题.
似乎错误在angular.js源中被return logFn.apply(console, args);线条抛出
12221.
关于现在该做什么的任何提示?
我正在尝试将我的应用程序从AngularJS v1.2迁移到v1.3或v1.4,但我正在TypeError: Illegal invocation使用Chrome(尝试过win,osx和ubuntu).
堆栈跟踪并没有真正帮助:
TypeError: Illegal invocation
at equals (angular.js:1034)
at equals (angular.js:1034)
at equals (angular.js:1034)
at equals (angular.js:1034)
at equals (angular.js:1034)
at Scope.$get.Scope.$digest (angular.js:15550)
at Scope.$get.Scope.$apply (angular.js:15824)
at done (angular.js:10263)
at completeRequest (angular.js:10435)
at XMLHttpRequest.requestLoaded (angular.js:10376)
Run Code Online (Sandbox Code Playgroud)
所以这似乎与AJAX请求有关,但我也得到:
TypeError: Illegal invocation
at equals (angular.js:1034)
at equals (angular.js:1034)
at equals (angular.js:1034)
at equals (angular.js:1034)
at equals (angular.js:1034)
at …Run Code Online (Sandbox Code Playgroud) 我已经为AngularJS编写了这个自定义过滤器,但是当它运行时,我得到了无限的摘要循环错误.为什么会发生这种情况,我该如何纠正?
angular.module("app", []).
filter('department', function(filterFilter) {
return function(items, args) {
var productMatches;
var output = [];
var count = 0;
if (args.selectedDepartment.Id !== undefined && args.option) {
for (let i = 0; i < items.length; i++) {
productMatches = items[i].products.filter(function(el) {
return el.Order__r.Department__r.Id === args.selectedDepartment.Id;
});
if (productMatches.length !== 0) {
output[count] = {};
output[count].products = productMatches;
output[count].firstProduct = items[i].firstProduct;
count++;
}
}
}
return output;
};
}).
Run Code Online (Sandbox Code Playgroud)
这是相关的HTML:
<tr class='destination' ng-repeat-start='pickupAccount in pickupAccounts | department : {"selectedDepartment": selectedDepartment, "option": …Run Code Online (Sandbox Code Playgroud) 在我的ng-app调试期间,我发现$ digest的大部分时间都需要regularInterceptedExpression.问题是,触发它的原因是什么?
我能够访问$scope每个接受的答案变量这里.但是,我无法从控制台编辑它,即更改属性,调用函数等.这甚至可能吗?
这是我一直在试验的测试代码:
<!doctype html>
<html data-ng-app="Foo">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module("Foo", []);
app.controller("One", ["$scope", function($scope) {
$scope.text = "hello";
}]);
</script>
</head>
<body>
<div id="container" ng-controller="One">
{{ text }}
</div><!-- #container -->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
如果我text使用控制台编辑属性,它会更改,但视图不会更改:
> angular.element($("#container")).scope().text
< "hello"
> angular.element($("#container")).scope().text = 'bye'
< "bye"
Run Code Online (Sandbox Code Playgroud)
如何$scope从控制台更改值和属性,以便视图和所有依赖项也获得更新?
我这里有一个小提琴:http://jsfiddle.net/KdkKE/44/
我想做的是创建一个'toggle'组件,基本上是一个自定义复选框,但如果它是真或假,则会更改html,它会绑定到控制器中的布尔值.
当用户单击切换时,模型会更新,指令的视图会发生变化.它类似于指令文档http://docs.angularjs.org/guide/directive末尾的示例,但状态将被绑定,以便在启动时正确.
var app = angular.module('App', []);
function Ctrl($scope) {
$scope.init = function() {
$scope.foo = true
}
}
app.directive('toggle', function() {
return {
restrict: 'E',
replace: true,
transclude: true,
scope: {
label: '@',
ngModel: '='
},
template:
'<div style="cursor: hand; cursor: pointer">{{label}}: {{ngModel}}</div>',
link: function(scope, element, attrs, controller) {
element.bind('click', function() {
scope.ngModel = false;
attrs.$set('ngModel', false);
console.log('plz', attrs.ngModel);
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
-
<div ng-app="App">
<div ng-controller="Ctrl" ng-init="init()">
<p>Foo in Ctrl: {{foo}}</p> …Run Code Online (Sandbox Code Playgroud) javascript angularjs angularjs-directive angularjs-scope angularjs-digest
所以我有一个AngularJS服务监听一些事件.在处理这些事件时,我需要调用不同的控制器并最终加载新视图.在一个事件处理程序中,我使用$ location.path()然后调用$ rootScope.apply()来触发到控制器的路由.这适用于该事件,但在其他情况下,我收到以下错误:$rootScope:inprog Action Already In Progress.我猜测它在第一个场景中有效,因为$ rootScope.apply()是从侦听器函数内的另一个回调函数调用的,其他处理程序试图仅从事件侦听器函数调用它.
//angular service
$rootScope.$on('MY_EVENT', function (event, msg) {
MyClass.doSomething(msg, function (response) {
$location.path("/view1");
$rootScope.$apply(); //WORKS FINE
});
});
$rootScope.$on('MY_OTHER_EVENT', function (event, msg) {
$location.path("/view2");
$rootScope.$apply(); //ERROR
});
Run Code Online (Sandbox Code Playgroud)
如何让它适用于所有事件处理程序?
如果首先更改范围属性,然后再广播事件,那么相应的观察者回调和事件监听器回调是否始终以相同的顺序执行?例如:
$scope.foo = 3;
$scope.$broadcast('bar');
Run Code Online (Sandbox Code Playgroud)
和其他地方:
$scope.$watch('foo', function fn1(){...});
$scope.$on('bar', function fn2(){...});
Run Code Online (Sandbox Code Playgroud)
将fn1永远执行fn2,反之亦然,或者订单不能依赖?请引用消息来源,最好是官方角度文档.
如果它很重要:让我们假设$ scope.foo =并且$ broadcast出现在由ng-click调用的函数中(即用户交互)
[旁白]抱歉问题标题很草率 - 如果你有更好的东西请重命名.
angularjs ×10
angularjs-digest ×10
javascript ×5
events ×2
cordova ×1
location ×1
performance ×1
rootscope ×1