Search code examples
javascripthtmlcssangularjsng-style

Angular CSS: ng-style working for one half, not other


I have an Angular application which is supposed to have a split screen with two halves, both of them with the same array values. The width and height values are set in a service and called by a controller. The style is set with ng-style on the DOM. However, the left half is not spaced out properly and the right half is. I have an image of the problem here:

enter image description here

Here is the relevant code:

riderSettingsHalf.html

<div ng-controller="RiderSettingsHalf">
    <table>
        <tbody>
            <tr class="rider-settings-half-bar" ng-repeat="item in items" ng-style="{ 'width': wdt, 'height': hgt }"><td>{{item}}</td></tr>
        </tbody>
    </table>
</div>

And here is all the relevant Angular code. I think the problem lies in the controller, but I've included the service and directive as well.

angular.module('app').controller('RiderSettingsHalf', ['$scope', 'BarSize', function($scope, BarSize){
    var BS = BarSize.getBar();
    $scope.wdt = BS.wdt;
    $scope.hgt = BS.hgt;


    var items = {
        trip1: "TRIP 1",
        trip2: "TRIP 2",
        rideData: "RIDE DATA",
        status: "VEHICLE STATUS",
        info: "VEHICLE INFO",
        audio: "AUDIO",
        bluetooth: "BLUETOOTH",
        image: "CUSTOM IMAGE"
    };

    $scope.items = items;
}]);

angular.module('app').directive('riderSettingsScreen', ['BarSize', function(BarSize){
    return {
        templateUrl: 'public/templates/riderSettingsHalf.html',
        link: function(scope, elem, attrs){
            var settingBarHeight = elem[0].parentNode.offsetHeight / 5;
            var settingBarWidth = elem[0].parentNode.offsetWidth;
            BarSize.setBar(settingBarHeight, settingBarWidth);
        }
    };
}]);

angular.module('app').service('BarSize', function(){
    var val = {};

    this.setBar = function(h, w){
        val = {hgt: h, wdt: w};
        console.log(val);
    };
    this.getBar = function(){
        return val;
    };
});

Solution

  • I simplified it and put it all in the directive:

    return {
        restrict: 'EA',
        templateUrl: 'public/templates/riderSettingsHalf.html',
        link: function(scope, elem, attrs){
            scope.wdt = elem[0].parentNode.offsetWidth;
            scope.hgt = elem[0].parentNode.offsetHeight / 5;
            scope.items = {
                trip1: "TRIP 1",
                trip2: "TRIP 2",
                rideData: "RIDE DATA",
                status: "VEHICLE STATUS",
                info: "VEHICLE INFO",
                audio: "AUDIO",
                bluetooth: "BLUETOOTH",
                image: "CUSTOM IMAGE"
            };   
        }
    };