1

我知道大多数人都这样使用风格:

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 );
    }
  };
});

我没有看到正在编译的样式,如何让样式显示?

到目前为止我的(中)完整代码: https ://plnkr.co/edit/5wwKJ445IqPGTNGGRDIv?p=preview

4

2 回答 2

1

我只能说width&height值应该px附加到它上面,否则它们将被视为无效的 CSS

$scope.stage.style = {width: '1155px', height: '1304px'}
于 2016-03-18T21:02:20.123 回答
-1

除了答案:

 // in directive
      if( !isNaN( scope.mystyle.width ) ){
        scope.mystyle.width += 'px';
      }
      if( !isNaN( scope.mystyle.height ) ){
        scope.mystyle.height += 'px';
      }
于 2016-03-18T21:10:21.067 回答