我想让元素A和元素B有相同的宽度。当B改变时,A也应该改变。
angularjs是怎么做的?
这里有一个小提琴来说明我的问题:http://jsfiddle.net/ADukg/6684/
<div ng-controller="MyCtrl">
<div class="red"></div>
<div class="blue" style="width: {{ // how do I get the red div's width? }}px"></div>
</div>
希望下面的链接有所帮助
http://plnkr.co/edit/h9MsdAUk0AFloHQu5eNu?p =
预览HTML代码
<body>
<h1>Hello Plunker!</h1>
<div ng-controller="MyCtrl">
<div id='red' class="red"></div>
<div class="blue" ng-style="{width:{{getRedWidth()}}}"></div>
</div>
</body>
Script.js文件
var app = angular.module('app', []);
app.controller('MyCtrl', function mainController($scope){
$scope.getRedWidth = function (){
var red = document.getElementById('red');
var redWidth = red.offsetWidth;
console.log(redWidth);
return "'"+redWidth+"px'";
}
});