barteloma
barteloma

Reputation: 6855

Setting angularjs ng-model value by an object value

I have an input text on my html side,

   <input ng-model="item.valueProperty"
       ng-disabled="item.options.length>0"
       type="text"
       placeholder="List item value"
       class="form-control"
       required>

if item.options array has any item, my textbox value (item.valueProperty) will be set as key But I want to this on html side via ng- directives.

I can not set it on javascript controller. is this possible?

Upvotes: 0

Views: 1584

Answers (1)

Carlos Arauz
Carlos Arauz

Reputation: 805

I think this is what you wantL

angular.module('test', [])
  .controller('test', function ($scope) {
    $scope.item = {
      valueProperty: 'Test',
      options: [1]
    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="test" ng-controller="test">
<input ng-model="item.valueProperty"
       ng-disabled="item.options.length > 0 ? item.valueProperty='key' : false"
       type="text"
       placeholder="List item value"
       class="form-control"
       required>
</div>

I just set $scope.item.valueProperty to 'key' if item.options.length > 0 in ng-disabled directive, item.options.length > 0 ? item.valueProperty=123 : falseis a valid expression, so it will work.

Upvotes: 2

Related Questions