gpt4 book ai didi

javascript - Angular js 两种方式数据绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 10:10:50 26 4
gpt4 key购买 nike

我的 Angular 应用程序的下拉列表中有一些值。我的要求是,当用户从下拉列表中选择任何特定值时,他将获得与该值对应的完整数组。

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-$filter-production</title>

<script src="//ajax.goo gleapis.com/ajax/libs/angularjs/1.4.0- rc.1/angular.min.js"></script>
<script>
(function (angular) {
'use strict';
angular.module('filterExample', [])
.controller('MainCtrl', function ($scope, $filter) {
$scope.originalText = [
{ name: "Object1", shape: "circle", color: "red" },
{ name: "Object2", shape: "square", color: "orange" },
{ name: "Object3", shape: "triangle", color: "yellow" },
{ name: "Object4", shape: "circle", color: "green" },
{ name: "Object5", shape: "sphere", color: "blue" },
{ name: "Object6", shape: "hexagon", color: "indigo" },
{ name: "Object7", shape: "square", color: "violet" },
{ name: "Object8", shape: "triangle", color: "red" }
]

//$scope.xxx = {d:'Object1'};
$scope.xxx = { d: null };
$scope.filteredText = $filter('filter')($scope.originalText, { name: $scope.xxx.d });
});
})(window.angular);
</script>
</head>

<body ng-app="filterExample">
<div ng-controller="MainCtrl">
<h3>{{ originalText }}</h3>
<h3>{{ filteredText }}</h3>

<select ng-model="xxx.d" ui-select2="">
<option ng-repeat="item in originalText" value="{{item.name}}">{{item.name}}</option>"
</select>

{{xxx.d}}

</div>
</body>
</html>

我的代码在 Plunker

在这里,我希望当用户在下拉列表中选择任何特定值时,他应该获得过滤后的数组。

最佳答案

你可以使用过滤器来实现这个目标

<div ng-controller="MainCtrl">
<h3>{{ originalText }}</h3>
<h3>{{ filteredText =(originalText| filter: {shape: xxx.shape}) }}</h3>
<select ng-model="xxx" ng-options="item as item.shape for item in originalText">
</select>
</div>

更新

您可以通过使用 ng-change 指令调用更改过滤方法来从 Controller 执行此过滤

标记

<select ng-model="xxx" ng-options="item as item.shape for item in originalText" ng-change="changeFunction()">
</select>

代码

$scope.changeFunction = function(){
$scope.filteredText = $filter('filter')($scope.originalText, { shape: $scope.xxx.shape}, true);
}

Working Plunkr

关于javascript - Angular js 两种方式数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30118279/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com