gpt4 book ai didi

javascript - Angular CSS : ng-style working for one half, 不是其他

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:36 27 4
gpt4 key购买 nike

我有一个 Angular 应用程序,它应该有一个分为两半的分屏,它们都具有相同的数组值。宽度和高度值在服务中设置并由 Controller 调用。样式在 DOM 上使用 ng-style 设置。但是,左半部分没有正确间隔,而右半部分是。我在这里有问题的图片:

enter image description here

相关代码如下:

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>

这里是所有相关的 Angular 代码。我认为问题出在 Controller 上,但我也包含了服务和指令。

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;
};
});

最佳答案

我简化了一下,全部放在了指令中:

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"
};
}
};

关于javascript - Angular CSS : ng-style working for one half, 不是其他,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37716603/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com