1
我有以下配置:UI路由器:處理嵌套控制器
.state("addUser", {
url: "/addUser",
templateUrl: "users/add-user.html",
controller: "AddUserParent",
controllerAs: "$ctrl",
abstract: true
})
.state("addUser.General", {
url: "/General",
templateUrl: "users/add-user-general.html",
controller: "AddUser",
controllerAs: "$ctrl",
})
.state("addUser.Cost", {
url: "/Cost",
templateUrl: "users/add-user-cost.html",
controller: "AddUser",
controllerAs: "$ctrl",
})
.state("addUser.Notes", {
url: "/Notes",
templateUrl: "users/add-user-notes.html",
controller: "AddUser",
controllerAs: "$ctrl",
})
而繼父控制器:
angular
.module("users")
.controller("AddUserParent", AddUserParent);
AddUserParent.$inject = ["usersSrv", "stateRouter"];
function AddUserParent(usersSrv, stateRouter) {
let $ctrl = this;
$ctrl.navigate = stateRouter.navigate;
$ctrl.user = {};
$ctrl.addUser = function() {
usersSrv.addUser($ctrl.user);
console.log($ctrl.user);
$ctrl.navigate('home');
}
}
兒童控制器AddUser
就是空的。當我填寫:
<input type="text" ng-model="$ctrl.user.name">
<input type="text" ng-model="$ctrl.user.location">
在addUser.General
狀態,並切換到addUser.Cost
我失去所有$ctrl.user
數據。爲什麼會這樣呢?如何解決這個問題?
在這裏你可以找到如何各國之間共享數據..作用域HTTP深解釋: //stackoverflow.com/a/27699798/1679310 –