我的 Controller 中有这个功能:
$scope.goToPath = function ( path, pid ) {
$scope.pid = pid;
console.log("scope pid : " + $scope.pid);
$scope.edit = true;
$state.go(path);
};
然后我调用这个函数来获得我工厂的 promise :
$scope.getProjectById = function () {
projectFactory.getProject($scope.pid)
.success(function (data) {
if(data == null){
$scope.errorMessage = "Le projet avec l'id : " + pid + " n'existe pas";
}else {
$scope.project = data;
}
})
.error(function (data, status, headers, config) {
$scope.errorMessage = "Erreur : " + data.error + ' ' + status;
})
};
我的工厂看起来像这样:
factory.getProject = function (projectId) {
console.log('Project Id factory : '+projectId);
return $http.get('http://localhost:8080/gestionprojet/Project/' + projectId)
};
return factory;
但是这里的变量projectId是空的,现在我已经用console.log()
检查过,我的 Controller 中的pid有一个值,但在工厂中它等于空
。
在我看来,我这样调用它:
ng-init="getProjectById()"
如有任何帮助,我们将不胜感激。谢谢
最佳答案
With this scenario,your current scope is getting destroyed and new scope is getting creates.So,
pid
assigned to scope is also getting destroyed.
你可以这样做。
在函数$scope.goToPath
中,
$scope.goToPath = function(path, pid ){
....
$state.go(path,{project_id:pid })
}
在您的 View 落地的目标 Controller 中,
通过注入(inject)使用 $stateParams
访问它。
您可以引用这里angular-ui路由
关于javascript - Controller ---工厂: passing parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39297717/