Oam*_*Psy 7 javascript jquery angularjs angularjs-directive angularjs-scope
我是新来AngularJS和最近推出它变成我的应用程序.我想重新写一些我现有的jQuery代码在我的控制器,但是,有一次,我使用:
jQuery的:
if ($(window).width() < 700) {
$('.productsHeading').on('click', function() {
$(".productsBody").hide();
$(".aboutUsBody").show();
});
}
Run Code Online (Sandbox Code Playgroud)
我可以避开.hide()与.show()使用ng-hide="productsBody"和ng-hide="aboutUsBody"我的DIV内.这些都是经过处理的ng-click="productsheading()".不过,我面临的问题是,我该如何处理:
if ($(window).width() < 700) {
Run Code Online (Sandbox Code Playgroud)
在AngularJS?我使用AngularJS V1.1.5
在AngularJS中,如果要对HTML或DOM操作进行更改,则建议或最佳实践是使用相同的指令.
写一个指令可能在下面链接是有帮助的:
http://jsfiddle.net/jaredwilli/SfJ8c/
指令的HTML
<div ng-app="miniapp" ng-controller="AppController" ng-style="style()" resize>window.height: {{windowHeight}}
<br />window.width: {{windowWidth}}
<br />
</div>Run Code Online (Sandbox Code Playgroud)
指令的Javascript代码
app.directive('resize', function ($window) {
return function (scope, element) {
var w = angular.element($window);
scope.getWindowDimensions = function () {
return {
'h': w.height(),
'w': w.width()
};
};
scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
scope.windowHeight = newValue.h;
scope.windowWidth = newValue.w;
scope.style = function () {
return {
'height': (newValue.h - 100) + 'px',
'width': (newValue.w - 100) + 'px'
};
};
}, true);
w.bind('resize', function () {
scope.$apply();
});
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7294 次 |
| 最近记录: |