0

First here's a plunk: http://plnkr.co/edit/2gD0AB. This plunk doesn't seem to work correctly because $scope.$on("$viewContentLoaded") won't fire, but on my machine it works just fine and I hope you get the idea.

What I'm trying to do there is simply move field objects from $scope.fields to $scope.groupFields = [], $scope.listFields = [], $scope.dataFields = [] when dragging them to the appropriate droppable areas. I've manager to do this using jQuery UI and jQuery UI touch punch (just to be mobile safe).

If you drag an element from the fields box to the one of the empty boxes you'll notice nothing happens on the screen besides the field elements hanging around where you left them. But if do a console.log($scope.fields) in the drop event listener you'll notice that all of the field objects are correctly set inside each of the field boxes.

If you click the Add element button then you'll simply trigger console.log($scope.groupFields); and suddenly all the elements appear correctly in their corresponding boxes like intended in the first place.

After pulling my brains out and after searching the internet for some time I found out that $scope.$apply(), called in the drop event after the moveField function, actually fixes my issue.

And I don't understand why. Shouldn't there be a digest already running and updating the view based on what I'm doing in the controller?

Thanks!

4

1 回答 1

0

仅仅因为处理这些 jQuerydrop事件的代码在控制器内部,并不意味着它将在 angularjs 范围/字下运行。为了使这些更改生效,您需要强制 angularjs 应用程序对其模型进行脏检查。这可以通过调用$apply()$digest()方法来完成。您应该始终在处理更改$scope属性的 jQuery 事件之后或在timeout(或interval)之后调用这些方法之一。

于 2013-01-27T02:09:48.983 回答