ng-style 作为传递给指令的变量

ng-style as a variable passed to directive

我知道大多数人都是这样使用样式的:

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

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

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

我只能说 widthheight 值应该附加 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';
      }