Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.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
AngularJS检测引导环境_Angularjs_Twitter Bootstrap_Window Resize - Fatal编程技术网

AngularJS检测引导环境

AngularJS检测引导环境,angularjs,twitter-bootstrap,window-resize,Angularjs,Twitter Bootstrap,Window Resize,我正在尝试使用AngularJS来检测引导环境。这是我的代码: angular.module("envService",[]) .factory("envService", envService); function envService($window){ return env(); //////////// function env(){ var w = angular.element($windo

我正在尝试使用AngularJS来检测引导环境。这是我的代码:

angular.module("envService",[])
    .factory("envService", envService);

    function envService($window){
        return env();

        ////////////

        function env(){
            var w = angular.element($window);
            var winWidth = w.width();
            if(winWidth<768){
                return 'xs';
            }else if(winWidth>=1200){
                return 'lg';
            }else if(winWidth>=992){
                return 'md';
            }else if(winWidth>=768){
                return 'sm';
            }
        }

    }
angular.module(“envService”,[])
.工厂(“环境服务”,环境服务);
功能服务($窗口){
返回env();
////////////
功能环境({
var w=角度元素($window);
var winWidth=w.width();
如果(winWidth=1200){
返回“lg”;
}否则如果(winWidth>=992){
返回“md”;
}否则如果(winWidth>=768){
返回“sm”;
}
}
}

该函数工作并根据窗口大小返回值。但是,即使更改了窗口大小,它也将始终返回相同的环境。如何修复它?

您需要注意窗口大小调整事件

angular.module('envService',[])
.factory('envFactory', ['$window', '$timeout', function($window, $timeout) {

var envFactory = {};
var t;

envFactory.getEnv = function () {
  var w = angular.element($window);
  var winWidth = w.width();
  if(winWidth<768){
      return 'xs';
  }else if(winWidth>=1200){
      return 'lg';
  }else if(winWidth>=992){
      return 'md';
  }else if(winWidth>=768){
      return 'sm';
  }        
};

angular.element($window).bind('resize', function () {
  $timeout.cancel(t);
  t = $timeout(function () {
    return envFactory.getEnv();
  }, 300); // check if resize event is still happening
});    

return envFactory;

}]);

angular.module('app',['envService']).controller('AppController', ['$scope', 'envFactory',
function($scope, envFactory) {
    // watch for changes
    $scope.$watch(function () { return envFactory.getEnv() }, function (newVal, oldVal) {
        if (typeof newVal !== 'undefined') {
            $scope.env = newVal;
            console.log($scope.env);
        }
    });

}
]);
angular.module('envService',[])
.factory('envFactory',['$window','$timeout',函数($window,$timeout){
var envFactory={};
变量t;
envFactory.getEnv=函数(){
var w=角度元素($window);
var winWidth=w.width();
如果(winWidth=1200){
返回“lg”;
}否则如果(winWidth>=992){
返回“md”;
}否则如果(winWidth>=768){
返回“sm”;
}        
};
angular.element($window.bind('resize',function(){
$timeout.cancel(t);
t=$timeout(函数(){
返回envFactory.getEnv();
},300);//检查调整大小事件是否仍在发生
});    
返回工厂;
}]);
角度.module('app',['envService']).controller('AppController',['$scope','envFactory',
功能($scope,envFactory){
//留意变化
$scope.$watch(函数(){return envFactory.getEnv()},函数(newVal,oldVal){
if(typeof newVal!=“未定义”){
$scope.env=newVal;
log($scope.env);
}
});
}
]);