這裏是我的代碼如何讓JavaScript從HTML而不是硬編碼數據讀取?
HTML:
<div ng-app="myFirstApp" ng-controller="countriesController">
<p> Search for a country: </p>
<p><input type="text" ng-model="test"></p>
<ul>
<li ng-repeat="x in countryList | filter:test">
{{ x.country }}
</li>
</ul>
</div>
的javascript:
var app = angular.module('myFirstApp', []);
app.controller('countriesController', function($scope) {
$scope.countryList = [
{country:'Argentina'},
{country:'Brazil'},
{country:'Chile'},
{country:'Colombia'},
{country:'Paraguay'},
{country:'Panama'},
{country:'Andorra'}
];
});
到目前爲止,我可以輸入到一個盒子裏,只返回匹配文本的國家。 我想做的事情。首先,如上所示,國家列表被硬編碼到JavaScript中。如果我有在HTML文件中像這樣一個標準的無序列表:
<ul id="countryList">
<li>Argentina</li>
<li>Brazil</li>
<li>Chile</li>
<li>Colombia</li>
<li>Paraguay</li>
<li>Panama</li>
<li>Andorra</li>
</ul>
我怎樣才能拿到劇本使用這個數據,而不是它是硬編碼到JS。 另一個較小的查詢是如何讓js做不同的模式。例如,如果我現在輸入'a',所有返回'a'的國家。但我只希望在這種情況下返回以A開頭的國家。
感謝
所以,你希望有一個「硬編碼」列表中的DOM(標記)和比JS讀出來?比爲什麼使用angularjs在第一個地方? - >你可以用jQuery或VanillaJS –