有两个控制器:
app.controller('comboController', function ($scope, $http) {
$http.get("http://InternetShop/php_files/supermarket.php?category=main").
success(function (data) {
$scope.categoryes = data;
console.log($scope.categoryes);
});
$scope.SetCategoryName = function(category){
console.log(category);
$scope.$broadcast('changeVal', category);
};
});
app.controller('tableController', function ($scope, $http) {
$scope.currentCombo = "Meat";
$scope.$on( 'changeVal', function(event, data ) {console.log(data);
$scope.currentCombo = data;
});
GetRequest($scope, $http, $scope.currentCombo);
});
function GetRequest($scope, $http, currentCombo){
$http.get("http://InternetShop/php_files/supermarket.php?category="+currentCombo).
success(function (data) {
$scope.products = data;
console.log($scope.products);
});
}
其逻辑是,当您单击下拉列表中的其中一个值时,表格应该更新。但没有任何效果。有人怀疑我在事件中做错了什么。这是视图:
<div ng-controller="comboController">
<div class="form-group col-sm-2" >
<select class="form-control">
<optgroup ng-repeat="category in categoryes track by $index">
<option ng-click="SetCategoryName(category)">{{category}}</option>
</optgroup>
</select>
</div>
<div class="col-sm-10" ng-controller="tableController">
<table class="table table-hover">
<thead>
<tr>
<th>id</th>
<th>Foto</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
</tr>
</thead>
<tbody >
<tr ng-repeat="product in products track by $index">
<td>{{product.id}}</td>
<td><img alt="alt" src="img/{{product.img}}"/></td>
<td>{{product.name}}</td>
<td>{{product.price}}</td>
<td>{{product.description}}</td>
</tr>
</tbody>
</table>
</div>
</div>
总的来说,我自己想通了。我没有在视图中正确处理事件。因此,重新设计标签设计
<select></select>并安装处理程序ng-change后,ng-model一切都像发条一样运转。编辑本身: