Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 控制器中的窗口调整设置变量不工作_Javascript_Jquery_Angularjs - Fatal编程技术网

Javascript 控制器中的窗口调整设置变量不工作

Javascript 控制器中的窗口调整设置变量不工作,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,在Angular中,我观察窗口大小,并在窗口宽度超过某个阈值时更改控制器上的“设备”变量: var myModule = angular.module("MyApp", []) .controller('MyController', function(UseHttp){ // store reference of "this" var self = this; function setDevice(){ var wWid

在Angular中,我观察窗口大小,并在窗口宽度超过某个阈值时更改控制器上的“设备”变量:

 var myModule = angular.module("MyApp", [])
    .controller('MyController', function(UseHttp){

       // store reference of "this"
       var self = this;

       function setDevice(){
          var wWidth = $(window).width();
          var theDevice;

          if (wWidth <= 991 && wWidth > 767) {
            theDevice = "narrow desktop";
          } else if (wWidth <= 767 && wWidth > 620) {
            theDevice = "iPad";
          } else if (wWidth <= 620 && wWidth > 500) {
            theDevice = "largeMobile";
          } else if (wWidth <= 500 && wWidth > 320) {
            theDevice = "mediumMobile";
          } else if (wWidth <= 320) {
            theDevice = "smallMobile";
          } else {
            theDevice = "desktop";
          }

          self.device = theDevice;          
      }

      //self.device gets set on window resize
      $(window).resize(function(){
        setDevice();
        $scope.$apply(); //but where do I store the var $scope?
      });

      //self.device gets set right away
      setDevice();

}).service('UseHttp', function($http) {....
var myModule=angular.module(“MyApp”,[])
.controller('MyController',函数(UseHttp){
//“this”的存储引用
var self=这个;
函数setDevice(){
var wWidth=$(window.width();
var设备;
if(wWidth 767){
设备=“窄桌面”;
}否则如果(wWidth 620){
设备=“iPad”;
}否则,如果(第500次世界大战){
设备=“大型移动设备”;
}否则如果(wWidth 320){
设备=“mediumMobile”;

}否则,如果(wWidth通过在resize事件中设置设备,则在更新模型时没有任何东西触发摘要循环。您需要使用$scope.$apply()手动执行此操作,如下所示:

$(window).resize(function() {
   setDevice();
   $scope.$apply();
});

我建议在setDevice()调用中执行$apply(),因为这实际上是作用域突变发生的地方。@YonaAppletree-在那里执行的问题是setDevice()的问题最初在摘要正在进行时被调用。唯一的问题是--在调整窗口大小时,我收到错误:“找不到变量:$scope”。我更新了上面的代码以显示我的完整控制器。我在哪里存储$scope变量?您只需在控制器中注入$scope,如下所示。controller('MyController',函数(UseHttp,$scope){。。。
$(window).resize(function() {
   setDevice();
   $scope.$apply();
});