2017-03-04 81 views
0

我有我的HTML端輸入文本NG-模型值,設置angularjs由對象值

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

如果item.options數組有任何項目,我的文本框的值(item.valueProperty)將被設置爲key 但我想通過ng-指令在html端進行此操作。

我無法在javascript控制器上設置它。這可能嗎?

+0

你能解釋一下你面臨的問題嗎? – Ved

+0

按選項長度設置輸入值。如果item.valueProperty的項目值爲「key」 – barteloma

+0

,那麼item.valueProperty的值爲「key」? – Ved

回答

2

我認爲這是你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>

我只設置$ scope.item.valueProperty以 '鑰匙',如果item.options.length> 0 NG-禁用指令,item.options.length > 0 ? item.valueProperty=123 : false是一個有效的表達式,所以它會起作用。

+0

是的,這是感謝,所以我學會了在HTML端設置模型值。 – barteloma

+0

是的,但似乎是一個不好的做法,嘗試使用控制器。好好享受! –