javascript - ng-style not updating width and height - Stack Overflow

i'm trying to create some circles that auto resize. the problem is that the ng-style attribute isn

i'm trying to create some circles that auto resize. the problem is that the ng-style attribute isn't updating the styles. see plunker

Javascript :

$scope.sellingPoints = {
    '1':{ 'text':'Newest Technologies'},
    '2':{ 'text':'Rapid Development'},
    '3':{ 'text':'Scaleable Solutions'},
    '4':{ 'text':'Custom CMS'}  
};

$scope.kpi = {};
$scope.sizes = {};
$scope.onResize = function() {
    console.log('joe');
    var kpifit = document.getElementById('fit');
    $scope.sizes.width = kpifit.offsetWidth - 30;
    $scope.kpi.width = $scope.sizes.width + 'px';
    $scope.kpi.height = $scope.sizes.width + 'px';  
    console.log($scope.sizes.width);
} 

angular.element($window).bind('resize', function() {
    $scope.onResize();
})
$timeout(function() {
    $scope.onResize();
}, 300);

html :

<div class="row no-margin">
    <div class="col-md-3 col-xs-6" id="fit" ng-repeat="item in sellingPoints">
        <div class="kpi" ng-style="{'width':kpi.width,'height':kpi.height}">
            <div class="kpi-text">{{item.text}}</div>
        </div>
    </div>
</div>

i'm trying to create some circles that auto resize. the problem is that the ng-style attribute isn't updating the styles. http://plnkr.co/edit/3HxSzWIyUsLUW6UwFlRR see plunker

Javascript :

$scope.sellingPoints = {
    '1':{ 'text':'Newest Technologies'},
    '2':{ 'text':'Rapid Development'},
    '3':{ 'text':'Scaleable Solutions'},
    '4':{ 'text':'Custom CMS'}  
};

$scope.kpi = {};
$scope.sizes = {};
$scope.onResize = function() {
    console.log('joe');
    var kpifit = document.getElementById('fit');
    $scope.sizes.width = kpifit.offsetWidth - 30;
    $scope.kpi.width = $scope.sizes.width + 'px';
    $scope.kpi.height = $scope.sizes.width + 'px';  
    console.log($scope.sizes.width);
} 

angular.element($window).bind('resize', function() {
    $scope.onResize();
})
$timeout(function() {
    $scope.onResize();
}, 300);

html :

<div class="row no-margin">
    <div class="col-md-3 col-xs-6" id="fit" ng-repeat="item in sellingPoints">
        <div class="kpi" ng-style="{'width':kpi.width,'height':kpi.height}">
            <div class="kpi-text">{{item.text}}</div>
        </div>
    </div>
</div>
Share Improve this question asked May 22, 2015 at 8:30 Sjoerd de WitSjoerd de Wit 2,4135 gold badges28 silver badges47 bronze badges 1
  • Is this code inside a directive? Or inside a normal page? If it's in a page, I think you should call controllerName.kpi.width or something in that order. – Steven Lemmens Commented May 22, 2015 at 8:33
Add a ment  | 

2 Answers 2

Reset to default 6

Handlers bound with angular.element.prototype.bind (and on) methods don't trigger digest by itself. So you need to do it manually:

angular.element($window).bind('resize', function() {
    $scope.onResize();
    $scope.$apply();
});

Demo: http://plnkr.co/edit/dPphd7KkQC7jNqleEgHb?p=info

If you want width value in ng-style using function

This below example is use for progress bar chart.

$scope.ChartDataList = [{
        Ch_Name: 'Document',
        Ch_Percentage: '40%',
        getMaxLength() {
            return '40%';
        }
    }, {
        Ch_Name: 'Video/Audio',
        Ch_Percentage: '66%',
        getMaxLength() {
            return '66%';
        }
    }
}]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745151028a4613868.html

相关推荐

  • javascript - ng-style not updating width and height - Stack Overflow

    i'm trying to create some circles that auto resize. the problem is that the ng-style attribute isn

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信