1

I am trying to make my input width respond to an AngularJS scope variable, by setting it's width conditionally with ng-style. I have gotten this to work beautifully with text-align, but for some reason it is not working with width...

The HTML:

<body ng-app="vfApp">
    <!--This works...  --> <input resize ng-style="{ 'width' : '100%' }" type="text"/>
    <!--This does not?!--> <input resize ng-style="{ 'width' : doResize ? '100%' : '10%' }" type="text"/>
</body>

The JS:

var vfApp = angular.module('vfApp', []);
vfApp.directive('resize', function () {
    return function ($scope) {
        $scope.doResize = false;
    };
});

EDIT: This is different from the suggested possible duplicate because I am not trying to apply a static CSS class, I am trying to use a variable to conditionally apply a style inline.

KenSchnetz
  • 206
  • 2
  • 12
  • 1
    Possible duplicate of [Angular ng-style with a conditional expression](http://stackoverflow.com/questions/19375695/angular-ng-style-with-a-conditional-expression) – Gonzalo.- Nov 17 '16 at 02:12
  • the solutions are different enough that it could not be a duplicate, though I'm not positive if that's enough to differentiate. – David Archibald Nov 17 '16 at 05:06

2 Answers2

1

I see you are using Angular 1.0.1. You can use this:

ng-style="doResize && {'width':'100%'} || {'width':'10%'}"

See demo below:

var vfApp = angular.module('vfApp', []);
vfApp.directive('resize', function($window) {
  return function($scope) {
    $scope.doResize = true;
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>

<body ng-app="vfApp">
  <!--This works...-->
  <input resize ng-style="{ 'width' : '100%' }" type="text" />
  <!--This does not?!-->
  <input resize ng-style="doResize && {'width':'100%'} || {'width':'10%'}" type="text" />

  <br/>isMobile value: {{doResize}}

</body>
kukkuz
  • 41,512
  • 6
  • 59
  • 95
  • @KenSchnetz let me know your feedback on this, thanks! – kukkuz Nov 17 '16 at 03:17
  • 1
    Perfect... what's wierd to me is that the way was doing it in my JSFiddle works with text-alignment no problem, but it doesn't work for width. Your solution however works great!! Thank you. – KenSchnetz Nov 17 '16 at 03:33
0

If you are aiming for the 100% width value, the problem is simple the ternary expression:

doResize ? '100%' : '10%'.

in your js file doResize is false. If you don't understand ternary expressions, they are a condensed if. The uncompressed form of your code is:

if(doResize) {
  return '100%';
} else {
  return '10%';
}

So you have two options to fix it:

  1. Change $scope.doResize = false; to $scope.doResize = true;
  2. Change the ternary expression to doResize ? '10%' : '100%';

Hope that helps.

David Archibald
  • 1,431
  • 14
  • 27
  • I understand the doResize is false, I condensed my overall code to solve this problem before implementing it in my website. Also, the problem isnt that I am not getting the 100% width, i'm not getting the 10% width either. Thank you for your time! – KenSchnetz Nov 17 '16 at 03:30
  • sorry, I misunderstood. – David Archibald Nov 17 '16 at 04:53
  • Please don't be sorry, I probably didn't explain it well enough!! Thank you so much for your time. – KenSchnetz Nov 17 '16 at 16:08