Lig*_*ted 48 resize directive window watch angularjs
我有暴露模块模式,看起来像这样:
'use strict';
angular.module('app', [])
.directive('myDirective', ['SomeDep', function (SomeDep) {
var linker = function (scope, element, attr) {
// some work
};
return {
link: linker,
restrict: 'E'
};
}])
;
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是将$ watch集成到这里.通过'$ window'服务专门观看窗口调整大小.
[编辑]:
我一直意识到我的问题是什么......当我忘记我将它作为属性实现时,我正在限制元素... @ _ @;
Mat*_*itt 89
你不应该需要$ watch.只需绑定到窗口上的resize事件:
'use strict';
var app = angular.module('plunker', []);
app.directive('myDirective', ['$window', function ($window) {
return {
link: link,
restrict: 'E',
template: '<div>window size: {{width}}px</div>'
};
function link(scope, element, attrs){
scope.width = $window.innerWidth;
angular.element($window).bind('resize', function(){
scope.width = $window.innerWidth;
// manuall $digest required as resize event
// is outside of angular
scope.$digest();
});
}
}]);
Run Code Online (Sandbox Code Playgroud)
ngu*_*yên 40
您可以resize在一些尺寸变化的地方聆听事件和火灾
指示
(function() {
'use strict';
angular
.module('myApp.directives')
.directive('resize', ['$window', function ($window) {
return {
link: link,
restrict: 'A'
};
function link(scope, element, attrs){
scope.width = $window.innerWidth;
function onResize(){
// uncomment for only fire when $window.innerWidth change
// if (scope.width !== $window.innerWidth)
{
scope.width = $window.innerWidth;
scope.$digest();
}
};
function cleanUp() {
angular.element($window).off('resize', onResize);
}
angular.element($window).on('resize', onResize);
scope.$on('$destroy', cleanUp);
}
}]);
})();
Run Code Online (Sandbox Code Playgroud)
在HTML中
<div class="row" resize> ,
<div class="col-sm-2 col-xs-6" ng-repeat="v in tag.vod">
<h4 ng-bind="::v.known_as"></h4>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
控制器:
$scope.$watch('width', function(old, newv){
console.log(old, newv);
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
96509 次 |
| 最近记录: |