我试图在angularjs应用程序中使用路由,如下所示:
app.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'}); }]);
controller.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; }); }; //....
index.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>
productList.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中使用。但是那我该怎么办?我该如何解决这个问题?
$element
$ element是不可注入的(它不是在AngularJS注入器中注册的东西),因此您不能以这种方式将其传递到控制器中。
由于ng- model指令,您的productsCtrl以及您的search()方法都可以访问所有表单数据,该指令在您的表单和控制器之间建立了双向绑定。例如,在search()方法内部,您已经可以使用$ scope.keywords这样的关键字。
看看这是否适合您:
var dt = $($scope.keywords).serialize(); // or maybe just $scope.keywords.serialize();
更新 :这个小提琴http://jsfiddle.net/michelpa/NP6Yk/似乎在控制器中注入了$ element。(我不确定这是如何/为什么工作……可能是因为控制器已附加到表单标签/指令上?)
更新#2 :可以将$ element注入到控制器中,但是“会与角度方式背道而驰” -https://groups.google.com/d/msg/angular/SYglWP_x7ew/lFtb75NWNWUJ
如评论中所述,我认为您问题的答案是将表单数据放入单个较大的对象中,并将其发送到$ http请求中。