【问题标题】:AngularJS / jQuery - How to append some properties of the $scope object in a jQuery event handlerAngularJS / jQuery - 如何在 jQuery 事件处理程序中附加 $scope 对象的一些属性
【发布时间】:2015-11-10 11:06:12
【问题描述】:

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

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

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

【问题讨论】:

    标签: jquery angularjs events angularjs-scope handler


    【解决方案1】:

    你必须使用 angularjs jq lite 包装器,同时注入 $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 使用自定义指令(它们适用于这些类型 :))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 2014-09-10
      相关资源
      最近更新 更多