私は以下のAngularJSコードを持っています。私は製品のリストを持っており、1つのコントローラを使用してリストから選択した製品を新規定義または更新できるようにしたいと考えています。以下は私のコードです。 newProductControllerの主な定義体が2回呼び出され、いくつかの理由でコントローラー定義ブロック/本体が何回実行されますか?
$scope.EditProduct = function(prodId) {
$location.path('/productedit/' + prodId);
};
:
私は単に呼び出しリスト内の製品を編集したい
。私は関数 "GetProductForEdit"が2回呼び出されることがわかります。私はすべてのプロジェクトを検索し、GetProductForEditへの呼び出しは1つしかありません。誰かがなぜこれが起こっているのか説明してもらえますか?これは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();
}]);
テンプレートに 'ng-controller =" newProductController "'があります。 –