我有以下AngularJS代码。我有一个产品列表,我希望能够使用一个控制器从列表中定义新的或更新选定的产品。以下是我的代码。控制器定义块/主体执行多少次?
当我想编辑列表中的一个产品,我只需拨打:
$scope.EditProduct = function(prodId) {
$location.path('/productedit/' + prodId);
};
出于某种原因,newProductController主要定义体被称为两次。我可以看到函数“GetProductForEdit”被调用两次。我搜索了所有的项目,并且只有一个调用GetProductForEdit。
有人可以解释为什么会发生这种情况?这是AJ中常见的行为吗?我究竟做错了什么?
非常感谢您的帮助。
Mehdi
我正在使用ngRoute。
http://127.0.0.1:5000/static/pages/index.html#/productedit/1
myApp.config(function ($routeProvider) {
$routeProvider
// some more routes
...
// route for the product edit page
.when('/productedit/:id', {
templateUrl: '../pages/productedit.html',
controller: 'newProductController'
});
});
myApp.controller('newProductController', ['$scope', '$http', '$routeParams', function ($scope, $http, $routeParams) {
....
$scope.GetProductForEdit = function(prodId) {/* code here is called twice*/};
$scope.Init = function() {
if ($routeParams.id != undefined && $routeParams.id != '') {
$scope.Operation = 'Update';
$scope.GetProductForEdit($routeParams.id);
} else {
$scope.Operation = 'New';
$scope.CleanupForm();
}
};
$scope.Init();
}]);
你可能在你的模板 –