0

我在尝试使用 angularjs 引用模型时遇到了问题。我可以让其中一些参考,但其他一些不起作用。我已包含以下代码:

function DeleteCustomers($scope, func) {
     $scope.delete = function() {
         func.deleteCustomer($scope.customer);
     }
 }


function UpdateCustomers($scope, func) {
    $scope.update = function() {
        func.updateCustomer({ name: $scope.name2, telephone: $scope.telephone2,
            email: $scope.email2, street: $scope.street2,
            city: $scope.city2, zipcode: $scope.zipcode2 });
        }
    }
}

html是

<div ng-controller="UpdateCustomers">
       <form class="test-angular-update">
           <input type="text" ng-model="name2"><br>
           <input type="text" ng-model="telephone2"><br>
           <input type="text" ng-model="email2"><br>
           <input type="text" ng-model="street2"><br>
           <input type="text" ng-model="city2"><br>
           <input type="text" ng-model="zipcode2"><br>
           <button class="button" ng-click="update()">Update</button><br>
       </form>
   </div>

   <br><br>
   <div ng-controller="DeleteCustomers">
       <form class="text-angular-delete">
           Customer Name <input type="text" ng-model="customer"><br>
           <button class="button" ng-click="delete()">Delete</button><br>
       </form>
   </div>

控制器无法引用 UpdateCustomers 和 DeleteCustomers 中的所有模型。任何帮助,将不胜感激。

谢谢!

4

1 回答 1

2

这是一个更可靠的应用程序结构,它可能有助于确保正确注入 func 服务。不确定这是否是您遇到的问题。

//app.js
var app = angular.module('app', []);

//services.js
app.factory('func', function(){/* service code*/});

//controllers.js
app.controller('DeleteCustomers', ['$scope', 'func', function($scope, func){
   $scope.delete = function() {
     func.deleteCustomer($scope.customer);
   };
}]);//edit had typo here missing ']'

编辑:工作演示http://jsfiddle.net/grendian/Um3pR/

于 2013-02-23T18:38:02.333 回答