Javascript 如何在AngularJS中为数组元素创建链接
我对AngularJS是新手。我想为数组中的元素创建链接。建议我一些代码或描述 app.jsJavascript 如何在AngularJS中为数组元素创建链接,javascript,angularjs,Javascript,Angularjs,我对AngularJS是新手。我想为数组中的元素创建链接。建议我一些代码或描述 app.js app.controller('ItemsController', function($scope) { $scope.message = 'Items are displayed'; $scope.items=['Appliances','Books','Cosmetics','Home & Furniture','Mens','women','kids']; }); it
app.controller('ItemsController', function($scope) {
$scope.message = 'Items are displayed';
$scope.items=['Appliances','Books','Cosmetics','Home & Furniture','Mens','women','kids'];
});
items.html
<div ng-controller="ItemsController">
<p><h1>Type a letter in the input field:</h1></p>
<p><input type="text" ng-model="test"></p>
{{message}}
<ul><h2>
<li ng-repeat="x in items| filter:test">
{{ x }}
</li>
</h2></ul>
</div>
在输入字段中键入字母:
{{message}}
-
{{x}
是上述代码的示例输出。输出中显示的项目应为链接。您可以使用
ui-sref
并将您的项目声明为应用程序中使用的状态,代码如下:
<ul>
<li ng-repeat="x in items| filter:test">
<a ui-sref="{{x}}"><h2>{{ x }}</h2></a>
</li>
</ul>
如果您使用的是布线,那么只需在li中创建一个锚定标记
<ul>
<h2>
<li ng-repeat="x in items| filter:test">
<a href="#/details/{{x}}">{{x}}</a>
</li>
</h2>
</ul>
项目应在控制器中作为routeParam提供,然后您可以相应地显示数据。
希望这有帮助。您可以像这样使用{{items[$index]}}>链接
<ul>
<h2>
<li ng-repeat="x in items| filter:test">
<a href="#/details/{{x}}">{{x}}</a>
</li>
</h2>
</ul>
myApp.config(['$routeProvider',function($routeProvider){
$routeProvider.
when('/details/:item',{
templateUrl:'partials/details.html',
controller: 'DetailsController'
}).
otherwise({
redirectTo : 'yourdefaultpath'
})