AngularJS 控制器 "as" 操作员

AngularJS controller "as" operator

我在 AngularJS 中有这个观点:

<form ng-submit="addCust.submit();"  ng-controller="AddCustomerCtrl as addCust">
<div>
    <input type="text" ng-model="addCust.cName" required />
</div>
<div>
    <input type="text" ng-model="addCust.cCity" required />
</div>
<div>
    <button type="submit">Add Customer</button>
</div>

我的控制器是:

helloWorldControllers.controller('AddCustomerCtrl',['$scope','$location',
function AddCustomerCtrl($scope, $location){
    $scope.submit = function(){
        $location.path('/addedCustomer/' + $scope.cName + "/" + $scope.cCity);
    };
}
]);

但是 "as" 运算符在我的 netbeans 中不起作用。

我的目标是将多个控制器附加到一个元素,所以我不想更改控制器本身的名称,而是根据需要在我想将控制器附加到元素的视图中更改。 任何人都可以帮助我哪里错了吗? 非常感谢。

将您的控制器修改为:

helloWorldControllers.controller('AddCustomerCtrl',['$location', function AddCustomerCtrl($location){ 
  var addCust = this;
  addCust.submit = function(){ $location.path('/addedCustomer/' + addCust.cName + "/" + addCust.cCity); }; } ]);