AngularJS / jQuery - 如何在 jQuery 事件处理程序中附加 $scope 对象的一些属性

AngularJS / jQuery - How to append some properties of the $scope object in a jQuery event handler

我遇到了问题。 我尝试在 jQuery 事件处理程序中附加 $scope 对象的一些属性。这是我的代码:

.controller('dashboard', ['$scope', function($scope){
    $( window ).resize(function(){
        $scope.device = ($(document).width() > 768) ? 'desktop' : 'mobile';
    });
}]);

但是设备变量似乎没有附加到仪表板控制器的 $scope 上。 我的代码有什么问题? 谢谢 :D

你必须使用 angularjs jq lite wrapper,同时注入 $window 和 $document 并相应地使用它们,

.controller('dashboard', ['$scope','$window','$document', function($scope){
     var windowjQ = angular.element($window),
         documentjQ = angular.element($document);

    windowjQ.bind('resize',function(){
        $scope.device = (documentjQ.width() > 768) ? 'desktop' : 'mobile';
        $scope.$apply() // to kick the digest cycle;
    });
}]);

注意:尝试对任何 dom 相关的内容使用自定义指令(它们适用于此类 :))