Javascript ng样式作为传递给指令的变量
我知道大多数人使用的风格如下:Javascript ng样式作为传递给指令的变量,javascript,html,css,angularjs,ng-style,Javascript,Html,Css,Angularjs,Ng Style,我知道大多数人使用的风格如下: ng-style="{'background-color': someVariable"} // or as a variable ng-style="stage.style" 我想要一个范围变量,我只是计算: // controller $scope.stage.style = stageFactory.init(); // returns Object {width: 1155, height: 1304} 我真正想做的是将其传递给指令: app.di
ng-style="{'background-color': someVariable"}
// or as a variable
ng-style="stage.style"
我想要一个范围变量,我只是计算:
// controller
$scope.stage.style = stageFactory.init(); // returns Object {width: 1155, height: 1304}
我真正想做的是将其传递给指令:
app.directive( 'stage', function( $window ){
return {
restrict: 'E',
transclude: true,
scope: {
mystyle: '='
},
template: '<div ng-style="mystyle" ng-transclude></div>',
link: function( scope, attr ){
if( ! scope.mystyle.width ){
scope.mystyle.width = $window.innerWidth;
}
if( ! scope.mystyle.height ){
scope.mystyle.height = $window.innerHeight;
}
// scope.mystyle = {
// width: scope.width,
// height: scope.height
// };
console.log( 'style in directive', scope.mystyle );
}
};
});
app.directive('stage',函数($window){
返回{
限制:'E',
是的,
范围:{
mystyle:“=”
},
模板:“”,
链接:功能(范围、属性){
如果(!scope.mystyle.width){
scope.mystyle.width=$window.innerWidth;
}
如果(!scope.mystyle.height){
scope.mystyle.height=$window.innerHeight;
}
//scope.mystyle={
//宽度:scope.width,
//高度:scope.height
// };
log('style in directive',scope.mystyle);
}
};
});
我没有看到的是正在编译的样式,如何让样式显示
到目前为止,我的完整代码:
我能说的就是
宽度
和高度
值应该附加px
,否则它们将被视为无效CSS
$scope.stage.style = {width: '1155px', height: '1304px'}
我只能说
宽度
和高度
值应该附加px
,否则它们将被视为无效CSS
$scope.stage.style = {width: '1155px', height: '1304px'}
除了答案之外:
// in directive
if( !isNaN( scope.mystyle.width ) ){
scope.mystyle.width += 'px';
}
if( !isNaN( scope.mystyle.height ) ){
scope.mystyle.height += 'px';
}
除了答案之外:
// in directive
if( !isNaN( scope.mystyle.width ) ){
scope.mystyle.width += 'px';
}
if( !isNaN( scope.mystyle.height ) ){
scope.mystyle.height += 'px';
}
@很高兴知道。。谢谢:-)@SoluableNonagon很高兴知道。。谢谢:-)