and*_*rew 30 html internet-explorer inline-styles angularjs angularjs-ng-repeat
我想根据角度控制器中函数的返回值设置div的位置
以下在FireFox和chrome中工作正常但在Internet Explorer中{{position($index)}}%被解释为文字字符串值,因此无效
<div ng-repeat="item in items" style="left:{{position($index)}}%"></div>
Run Code Online (Sandbox Code Playgroud)
以下是此问题的示例:
var app = angular.module('app', []);
app.controller('controller', function($scope) {
$scope.items=[1,2,3,4,5,6,7,8,9,10];
$scope.position=function(i){
var percent =[5,10,15,20,25,30,35,40,45,50,55,60,65,70];
return percent[i+1];
}
});
Run Code Online (Sandbox Code Playgroud)
这是一个小提琴演示
有没有人有关于如何纠正的建议?
PSL*_*PSL 49
你必须使用ng风格而不是样式,否则一些浏览器IE会删除无效的样式属性值({{}}等等的存在使其无效)甚至角度有机会渲染它.使用ng-styleangular时,将计算表达式并为其添加内联样式属性.
<div ng-repeat="item in items" ng-style="{left: position($index) + '%'}"></div>
Run Code Online (Sandbox Code Playgroud)
既然你无论如何计算你的位置,你也可以%从中添加position并发送它.还要记住,在ng-repeat中调用函数会在每个摘要周期调用该函数,因此您可能需要注意不要在方法内部执行过多的密集操作.
<div ng-repeat="item in items" ng-style="{left: position($index)}">{{item}}</div>
Run Code Online (Sandbox Code Playgroud)
并返回
return percent[i+1] + "%";
Run Code Online (Sandbox Code Playgroud)
Sum*_*ick 30
如果你想像{{}}正常的样式属性一样使用角度绑定表达式style="width:{{someScopeVar}}",使用ng-attr-style它将完美地工作IE(显然其他更智能的):)
检查我的jsFiddle ...检查Angular JS 1.4.8
在这里我已经表明的使用style,ng-style以及ng-attr-style
HTML
<div ng-app="app">
<div ng-controller="controller">
<div style="background:{{bgColor}}">
This will NOT get colored in IE
</div>
<div ng-attr-style="background:{{bgColor}}">
But this WILL get colored in IE
</div>
<div ng-style="styleObject">
And so is this... as this uses a json object and gives that to ng-style
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
JS
var app = angular.module('app', []);
app.controller('controller', function($scope) {
$scope.bgColor = "yellow";
$scope.styleObject = {"background": "yellow"};
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15686 次 |
| 最近记录: |