为什么我不能将ng-style设置为节点?

时间:2013-11-14 16:16:02

标签: angularjs

我正在尝试设置ng风格:

的javascript:

$offset='top:'+info.scrollTop+'px';
$scope.scrollTop = $offset;
console.log($scope);

标记:

    <div ng-controller="Legal" ng-style="scrollTop" class="footer">

console.log上的作用域包含一个名为scrollTop的节点,其值为"top:0px"

2 个答案:

答案 0 :(得分:0)

试试这个(颜色有效,但顶部我不确定):

JavaScript的:

angular.module('App', []);

function Legal($scope) {
  var offset = {color:'red', top: '100px'};
  $scope.scrollTop = offset;
  console.log($scope.scrollTop);
}

HTML:

<!doctype html>
<html ng-app="App">

<head>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
  <script type="text/javascript" src="script.js"></script>
</head>

<body>
  <div ng-controller="Legal" ng-style="scrollTop" class="footer">Test {{scrollTop}}</div>
</body>

</html>

Plunker example

答案 1 :(得分:0)

ngStyle指令接受一个对象,其样式名称为键,而不是字符串:

$offset= info.scrollTop+'px';
$scope.scrollTop = {top: $offset};

然后您的标记将起作用:

<div ng-controller="Legal" ng-style="scrollTop" class="footer">

Fiddle