Angularjs ng-model设定载荷值?

时间:2013-12-02 09:41:30

标签: angularjs

我正在使用Angular js,我在typeahead中创建了一个带有列表的输入字段:

<input type="materialid" ng-model="material"  
typeahead="material as material.ID for material in materials | 
filter:{ID:$viewValue} | limitTo:10"  
typeahead-editable="false" typeahead-min-length="0" />

我的愿望是在页面加载时自动设置一个值(在列表中)。

例如,如果材料清单是{hammer, handsaw, screwdriver, drill-bit...},我想在加载时自动选择“螺丝刀”。

编辑:

我还不够具体。 这是我的html(我的表格的一部分)

   <tbody ng-repeat="line in linesTest">
      <tr>
        <td>    
          <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="monid{{line.id}}" checked="{{line.checked}}">
        </td>
        <td>
          <input type="materialid" ng-model="material" ng-init="{{line.type}}"  typeahead="material as material.ID for material in materials | filter:{ID:$viewValue} | limitTo:10"  typeahead-editable="false" typeahead-min-length="0" /><br/>{{material.LABEL}}
        </td>
        <td>
           <input class="poids" type="emptyload" ng-model="line.tar" ng-model="emptyload"/>
        </td>
        <td>
           <input class="poids" type="load" ng-model="line.pesee"/>
        </td>
        <td>
           <input class="poids" type="load" ng-model="line.poids"/>
        </td>
    </tr>
 </tbody>

这是我的js:

$http.get('http://192.168.1.3/pesoweb/webservice/rest/materials',{ cache: true }).
success(function(dataList) {
    $scope.materials = dataList;
});

var id = 0;

$scope.linesTest = [
{id:id++, checked: "checked", type: "screwdrivers", tar:"0125", pesee:"21000", poids:"24545"},
{id:id++, checked: "", type: "handsaw", tar:"52632", pesee:"4524", poids:"24545"},
{id:id++, checked: "", type: "ALPHA", tar:"548485", pesee:"68465", poids:"75421"},
{id:id++, checked: "checked", type: "hammer", tar:"7825", pesee:"23541", poids:"74"}
];

$scope.addEmptyRow = function() {
  $scope.linesTest.push({id:id++, checked: "checked", type: "", tar:"", pesee:"", poids:""});
};

$scope.removeRow = function(id) {
  $scope.linesTest.remove(id);
};

我想在我的表中为我的数组中的每一行创建一行。 $ scope.materials是一个JSON数组。

你现在明白了吗?

2 个答案:

答案 0 :(得分:3)

您可以在controller

中执行此操作
$scope.material = 'screwdriver';

答案 1 :(得分:0)

使用

ng-init="material = 'screwdriver''"

<input type="materialid" ng-model="material" ng-init="material = 'screwdriver''"  
typeahead="material as material.ID for material in materials | 
filter:{ID:$viewValue} | limitTo:10"  
typeahead-editable="false" typeahead-min-length="0" />