angularjs,uirouter-在控制器更新时更新ng模型?
视图1: 视图2:angularjs,uirouter-在控制器更新时更新ng模型?,angularjs,angular-ui-router,Angularjs,Angular Ui Router,视图1: 视图2: $scope.selectedList = { name: "" }; $scope.goToForm = function(e) { $scope.selectedList.name = e.name; $state.go('view2'); console.log(e); // prints updated value }; 但是输入框始终为空,即使要访问视图,调用了g
$scope.selectedList = {
name: ""
};
$scope.goToForm = function(e) {
$scope.selectedList.name = e.name;
$state.go('view2');
console.log(e); // prints updated value
};
但是输入框始终为空,即使要访问视图,调用了goToForm()。为什么它不更新HTML值?
视图随ui.router的$state更改。如果您做得不对,您正在更改
状态
,因此必须传递数据,否则将丢失数据
<div ng-controller="ctrl1">
<input
id="name"
name="name"
type="text"
ng-model="selectedList.name"
ng-readonly="true"
/>
</div>
并在ctrl1.js
中放置一个init
函数,检查是否有任何值作为$stateParam
传递。如果有,请相应分配
$scope.goToForm = function(e) {
var obj = {name: e.name};
$state.go('view2',obj);
console.log(e); // prints updated value
};
或您可以在
根据您的代码,将默认变量初始化包装到方法中
并调用view1的ng init,忽略view2的ng init
您的控制器应如下所示
和view1应包括ng init指令,如下所示
什么是goToExtendedForm
?将$stateParam添加到ctrl1,抛出未知的提供程序“stateParamProvider”errorconsole.log($stateParams);in init()返回“#”:null
?变量不存在:S@user3761308因为您还需要在stateProvider
中定义。检查。我想你错过了too@user3761308有关更多信息,请更新“view2”ui路由器配置部分?此外,在view2中,您正在使用加载整个控制器ctrl1的命令,其中代码$scope.selectedList.name设置为“”,行为$scope.selectedList={name:“”@user3761308:使用ng init方式更新
$scope.goToForm = function(e) {
var obj = {name: e.name};
$state.go('view2',obj);
console.log(e); // prints updated value
};
app.controller('ctrl1',function($stateParams){
init(); // your first line of controller
// then all your code from here
function init(){
if($stateParams.name) {
$scope.selectedList.name = $stateParams.name;
}
}
})
$scope.initializeVariables=function(){
$scope.selectedList= {name : ""}
}
$scope.goToForm = function(e) {
$scope.selectedList.name = e.name;
$state.go('view2');
console.log(e); // prints updated value
};
<div ng-controller="ctrl1" ng-init="initializeVariables();">
<button ng-click="goToForm ({'name':'aaa'})">
</button>
</div>