javascript 변수에 액세스 한 다음 컨트롤러에서 변경 한 다음 경로에서 사용하려고합니다.하지만 동일한 이전 변수를 표시합니다.
var userEditid = 0;
app.controller("cn_newuser", function ($scope,$window) {
editUser = function (this_) {
$window.userEditid = this_.firstElementChild.value;
alert(userEditid);
//window.location.path("#/edit_user");
//$window.location.href = "#/edit_user";
}
노선
.when("/edit_user", {
templateUrl: "/master/edituser/" + userEditid //userEditid should be 3 or some thing else but is showing 0
})
abobe 경로에서 userEditid는 3 또는 다른 것이어야하지만 0을 표시합니다.
이는 컨트롤러가 실행될 때가 .when()
아니라이 전역 변수 (아마도 좋은 생각이 아님)가로 설정 될 때 앱 인스턴스화에서 평가 되기 때문 0
입니다. "템플릿을로드하지만 템플릿 이름은 특정 변수에 따라 달라야합니다."라고 말하려는 경우 작업 방식이 작동하지 않습니다.
여기서 두 가지를 수행합니다.
서비스의 변수 :
app.controller("cn_newuser", function ($scope,UserIdService) {
$scope.editUser = function (this_) {
UserIdService.userEditid = this_.firstElementChild.value;
$location.path('/edit_user');
}
});
또한 사용하도록 변경했습니다. $location.path()
마스터 템플릿 :
.when("/edit_user", {
templateUrl: "/master/edituser.html", controller: 'EditUser'
});
EditUser 컨트롤러 :
app.controller("EditUser", function ($scope,UserIdService) {
$scope.userTemplate = '/master/edituser/'+UserIdService.userEditId;
});
그리고 edituser.html은 다음과 같습니다.
<div ng-include="userTemplate"></div>
물론 Angular에 의해 동적으로 수정되는 단일 템플릿이 아닌 사용자별로 별도의 템플릿을 원하는 이유를 묻 겠지만, 요청한 내용은 아닙니다.
편집 :
서비스는 다음과 같습니다.
app.factory('UserIdService',function() {
return {
userEditId: null
}
});
저것과 같이 쉬운.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다