Javascript Angular.js ng风格赢得';t绑定值
我对angularjs有问题,即使经过研究,我也找不到我错的地方 我需要重新计算元素的css值“left”。我使用的是'ng style'指令和一个方法,它将返回一个带有css值的对象。这是我必须做的。但是当我更新值时,它不会更新样式 ng绑定用法: 方法来更改对象Javascript Angular.js ng风格赢得';t绑定值,javascript,angularjs,Javascript,Angularjs,我对angularjs有问题,即使经过研究,我也找不到我错的地方 我需要重新计算元素的css值“left”。我使用的是'ng style'指令和一个方法,它将返回一个带有css值的对象。这是我必须做的。但是当我更新值时,它不会更新样式 ng绑定用法: 方法来更改对象 $scope.nextPosition=function(){ 如果((this.currentPosition+1)Thx,谢谢你的时间!我用Cherniv的输入解决了这个问题,但我不确定如何解决。我改变了创建值的方式。现在它开始
$scope.nextPosition=function(){
如果((this.currentPosition+1)Thx,谢谢你的时间!我用Cherniv的输入解决了这个问题,但我不确定如何解决。我改变了创建值的方式。现在它开始工作了
$scope.calcCssShift = function(){
this.cssShift = ($scope.currentPosition * $scope.slideSize)*-1;
};
$scope.getCssShiftObject =function(){
return {'left':$scope.cssShift+'px'};
};
$scope.nextPosition = function(){
if((this.currentPosition+1) <= this.maxPosition){
$scope.currentPosition = this.currentPosition+1;
$scope.calcCssShift();
}
};
$scope.calcCssShift=function(){
this.cssShift=($scope.currentPosition*$scope.slideSize)*-1;
};
$scope.getCssShiftObject=函数(){
返回{'left':$scope.cssShift+'px'};
};
$scope.nextPosition=函数(){
如果((this.currentPosition+1)我遇到了类似的问题。我试图使用ngStyle加载背景图像,但是如果表达式中的变量不能立即使用(如果它是资源承诺的一部分,可能会出现这种情况),它将无法工作
为了解决这个问题,我创建了自己的ngStyle指令来解决这个问题。希望这比为每个场景创建函数要好,在每个场景中,您都希望以这种方式使用ngStyle
app.directive("myStyle", function (){
return {
restrict: 'A',
link: function(scope, element, attrs)
{
var el = element[0],
attr = el.getAttribute('style');
el.setAttribute('style', attr);
// We need to watch for changes in the style in case required data is not yet ready when compiling
attrs.$observe('style', function (){
attr = el.getAttribute('style');
if(attr)
{
el.setAttribute('style', attr);
}
});
}
};
});
然后,您可以这样使用它:
<a my-style style="background-image: url('{{promise.myImage}}')"></a>
我的style属性也有类似的问题。我的绑定在某些浏览器中不起作用,特别是IE。我使用ng attr style=“{yourBindingExpression}}”解决了这个问题
阅读有关ng attr插值的更多信息,请访问谁和何时调用$scope.nextPosition
?您可以添加小提琴或弹拨器吗?它由ng单击指令调用我看不到任何问题:
$scope.calcCssShift = function(){
this.cssShift = ($scope.currentPosition * $scope.slideSize)*-1;
};
$scope.getCssShiftObject =function(){
return {'left':$scope.cssShift+'px'};
};
$scope.nextPosition = function(){
if((this.currentPosition+1) <= this.maxPosition){
$scope.currentPosition = this.currentPosition+1;
$scope.calcCssShift();
}
};
app.directive("myStyle", function (){
return {
restrict: 'A',
link: function(scope, element, attrs)
{
var el = element[0],
attr = el.getAttribute('style');
el.setAttribute('style', attr);
// We need to watch for changes in the style in case required data is not yet ready when compiling
attrs.$observe('style', function (){
attr = el.getAttribute('style');
if(attr)
{
el.setAttribute('style', attr);
}
});
}
};
});
<a my-style style="background-image: url('{{promise.myImage}}')"></a>