我对 Angular.js 的 $scope 依赖注入有很多疑问
但首先我想向您展示我创建的两个示例,
第一个例子:如下,这里我在一个表中搜索员工,代码如下,
<!DOCTYPE html>
<html ng-app="EmpApp">
<head>
<title>Plain Template</title>
<script type="text/javascript" src="D:/RahulShivsharan/Installers/AngularJS/angular.js"></script>
<script type="text/javascript">
var empApp = angular.module("EmpApp",[]);
empApp.controller("empCtrl",function($scope){
$scope.employees = [
{
"name" : "Manoj Bhandgar",
"age" : 23,
"profession" : "Computer Engineer",
"salary" : 30000
},
{
"name" : "Ajith Murgadoss",
"age" : 33,
"profession" : "Mechanical Engineer",
"salary" : 60000
},
{
"name" : "Swati Nandgaokar",
"age" : 28,
"profession" : "Electrical Engineer",
"salary" : 45000
},
{
"name" : "Fahim Ansari",
"age" : 49,
"profession" : "Advertising",
"salary" : 90000
}
];
});
</script>
</head>
<body ng-controller="empCtrl">
<table>
<tr>
<td align="right">Search Employee :</td>
<td><input type="text" ng-model="query"></td>
</tr>
</table>
<hr />
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Profession</th>
<th>Salary</th>
</tr>
<tr ng-repeat="emp in employees | filter: query">
<td>{{emp.name}}</td>
<td>{{emp.age}}</td>
<td>{{emp.profession}}</td>
<td>{{emp.salary}}</td>
</tr>
</table>
</body>
第二个例子:现在我根据搜索类型搜索员工,代码如下,
<!DOCTYPE html>
<html ng-app="EmpApp">
<head>
<title>Plain Template</title>
<script type="text/javascript" src="D:/Rahul Shivsharan/Installers/AngularJS/angular.js"></script>
<script type="text/javascript">
var empApp = angular.module("EmpApp",[]);
empApp.controller("empCtrl",function($scope){
$scope.employees = [
{
"name" : "Manoj Bhandgar",
"age" : 23,
"profession" : "Computer Engineer",
"salary" : 30000
},
{
"name" : "Ajith Murgadoss",
"age" : 33,
"profession" : "Mechanical Engineer",
"salary" : 60000
},
{
"name" : "Swati Nandgaokar",
"age" : 28,
"profession" : "Electrical Engineer",
"salary" : 45000
},
{
"name" : "Fahim Ansari",
"age" : 49,
"profession" : "Advertising",
"salary" : 90000
}
];
$scope.prop = {};
$scope.query = {};
});
</script>
</head>
<body ng-controller="empCtrl">
<table>
<tr>
<td align="right">Search Employee :</td>
<td><input type="text" ng-model="query[prop]"></td>
</tr>
<tr>
<td align="right">Search By :</td>
<td>
<select ng-model="prop">
<option value="$">ALL</option>
<option value="name">Name</option>
<option value="age">Age</option>
<option value="profession">Profession</option>
<option value="salary">Salary</option>
</select>
</td>
</tr>
</table>
<hr />
<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Profession</th>
<th>Salary</th>
</tr>
<tr ng-repeat="emp in employees | filter: query">
<td>{{emp.name}}</td>
<td>{{emp.age}}</td>
<td>{{emp.profession}}</td>
<td>{{emp.salary}}</td>
</tr>
</table>
</body>
在以上两种情况下,我都有一些问题,
在第一个示例中,我将“查询”创建为 ng-model,但未明确添加到 $scope,为什么?
但是在第二个示例中,我在 $scope 中添加了“查询”和“道具”,为什么?
在这两种情况下过滤器:查询而不是过滤器:查询 [prop],为什么?
我知道 prop 用于保存值“name”,.. 等,并且 query.name 或 query.salary 应该是这种情况,但它在 angular.js 代码下是如何工作的。
我想要一个很好的解释