我正在尝试在 angularjs 应用程序中使用路由,如下所示:
应用程序.js
angular.module('productapp', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/productapp', {templateUrl: 'partials/productList.html', controller: productsCtrl}).
//~ when('/productapp/:phoneId', {templateUrl: 'partials/phone-detail.html', controller: PhoneDetailCtrl}).
otherwise({redirectTo: '/productapp'});
}]);
控制器.js
function productsCtrl($scope, $http, $element) {
//~ $scope.url = 'php/search.php'; // The url of our search
// The function that will be executed on button click (ng-click="search()")
$http.get('php/products.php').success(function(data){
alert("hi");
$scope.products = data;
});
$scope.search = function() {
var elem = angular.element($element);
var dt = $(elem).serialize();
dt = dt+"&action=index";
alert(dt);
console.log($(elem).serialize());
$http({
method: 'POST',
url: 'php/products.php',
data: dt,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data, status) {
//console.log(data);
$scope.products = data; // Show result from server in our <pre></pre> element
}).error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});
}; //....
索引.html
<html ng-app = "productapp">
<head>
<title>Search form with AngualrJS</title>
<script src="../angular-1.0.1.min.js"></script>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="js/products.js"></script>
<script src="js/app.js"></script>
</head>
<body>
<div ng-view></div>
</body>
</html>
产品列表.html
<div>
<label>Search</label>
<input type="text" name="searchKeywords" ng-model="keywords" placeholder="enter name..." value="{{rs.name}}">
<button type="submit" ng-click="search()">Search</button>
<label>Add New Product:</label>
<input type="text" name="keywords" ng-model="rs.name" placeholder="enter name..." value="{{rs.name}}">
<input type="text" name="desc" ng-model="rs.description" placeholder="enter description..." value="{{rs.description}}">
<button type="submit" ng-click="add()">Add</button>
<button type="submit" ng-click="save(rs.product_id)">Save</button>
<p>enter product name...</p>
<table border='2'>
<th>Name</th>
<th>Description</th>
<th>Edit</th>
<th>Delete</th>
<tr ng-repeat="product in products" ng-model = 'products'>
<td>{{product.name}}</td>
<td>{{product.description}}</td>
<td><a href='' ng-click = "fetch(product.product_id)">edit</a></td>
<td> <a href='' ng-click = "del(product.product_id)" ng-hide="isHidden">delete</a></td>
</tr>
</table>
</div>
当我运行此代码时,我在控制台(谷歌浏览器)中收到以下错误:
Error: Unknown provider: $elementProvider <- $element
我知道发生此错误是因为我$element
在 productsCtrl 中使用。但那我该怎么办?我该如何解决这个问题?