错误:未知提供者:$ elementProvider < - $ element

z22*_*z22 6 routing angularjs

我试图在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'});
}]);
Run Code Online (Sandbox Code Playgroud)

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;
        });
    }; //....
Run Code Online (Sandbox Code Playgroud)

的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> 
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,我在控制台(谷歌浏览器)中收到以下错误:

Error: Unknown provider: $elementProvider <- $element
Run Code Online (Sandbox Code Playgroud)

我知道发生了这个错误,因为我正在使用$elementproductsCtrl.但那我该怎么办?我该如何解决这个问题?

Mar*_*cok 4

$element 不可注入(它不是用 AngularJS 注入器注册的东西),所以你不能以这种方式将它传递到你的控制器中。

由于 ng-model 指令,您的 productsCtrl 以及您的 search() 方法可以访问所有表单数据,该指令在表单和控制器之间设置了双向绑定。例如,在您的 search() 方法中,您已经可以访问关键字 $scope.keywords。

看看这是否适合您:

var dt = $($scope.keywords).serialize();  // or maybe just  $scope.keywords.serialize();
Run Code Online (Sandbox Code Playgroud)

更新:这个小提琴http://jsfiddle.net/michelpa/NP6Yk/似乎将 $element 注入到控制器中。(我不确定它是如何/为什么起作用的......也许是因为控制器附加到表单标签/指令?)

更新#2:将 $element 注入控制器是可能的,但是“深入反对角度方式” - https://groups.google.com/d/msg/angular/SYglWP_x7ew/lFtb75NWNWUJ

正如评论中提到的,我认为您问题的答案是将表单数据放入一个更大的对象中,然后将其发送到您的 $http 请求中。