Angular.js:基于用户输入如何改变的div宽度宽度、用户、Angular、js

2023-09-13 03:25:51 作者:瞄准你的心脏

我有接受格宽度值的输入框。使用angular.js,如何改变基于用户输入的div的宽度?我已经实现以下提到这个捣鼓后,code。 http://jsfiddle.net/311chaos/vUUf4 / 4 /

I have an input box which accepts the value for div width. Using angular.js, How can you change div's width based on user input? I have implemented following code after referring this fiddle.http://jsfiddle.net/311chaos/vUUf4/4/

标记:

 <input type="text" id="gcols" placeholder="Number of Columns" ng-model="cols" ng-change="flush()"/>

<div getWidth rowHeight=cols ng-repeat="div in divs">{{$index+1}} aaaaaa</div>

controller.js

var grid = angular.module('gridApp', []);

grid.controller('control', ['$scope', function ($scope) {

    /* code for repeating divs based on input*/
    $scope.divs = new Array();
    $scope.create=function(){ //function invoked on button's ng-click
            var a = $scope.cols;
            for(i=0;i<a;i++)
            {
                $scope.divs.push(a);
            }
            alert($scope.divs);
        };


}]);

grid.directive('getWidth', function () {
    return {
        restrict: "A",
        scope: {
            "rowHeight": '='
        },
        link: function (scope, element) {

            scope.$watch("rowHeight", function (value) {
                console.log(scope.rowHeight);
                $(element).css('width', scope.rowHeight + "px");
            }, false);
        }
    }
});

function appCtrl($scope) {
    $scope.cols = 150;   //just using same input value to check if working


}

更新我的最终目标是要设置DIV的宽度。当我打电话联CSS像下面我达到我想要的。

UPDATE My ultimate goal is to set width of that div. When I call inline CSS like following I achieve what I want.

<div get-width row-height="{{cols}}" ng-repeat="div in divs" style="width:{{cols}}px">{{$index+1}} aaaaaa</div>

但是,这不会是如果div的数目变高总是有效。所以,再次问题是,如何通过角脚本做到这一点?

But this wouldn't be always efficient if number of divs goes high. So, again question remains, how to do this via angular script?

推荐答案

使用 NG-风格={宽度:{{COLS}} +'像素'} ,而不是风格。

链接文档:http://docs.angularjs.org/api/ng.directive:ngStyle