Angular.js:如何根据用户输入更改div宽度 [英] Angular.js: How to change div width based on user input

查看:592
本文介绍了Angular.js:如何根据用户输入更改div宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入框接受div宽度的值。
使用angular.js,如何根据用户输入更改div的宽度?
我在引用这个小提琴后实现了以下代码。 http://jsfiddle.net/311chaos/ vUU4 / 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


}

UPDATE
我的最终目标是设置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-style =

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

这篇关于Angular.js:如何根据用户输入更改div宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆