Angularjs ng模型在ui路由器嵌套视图中不工作
我有以下代码结构 index.htmlAngularjs ng模型在ui路由器嵌套视图中不工作,angularjs,angular-ui-router,Angularjs,Angular Ui Router,我有以下代码结构 index.html <body ng-app="jobPortalApp" ng-controller="mainController"> <div ui-view></div> </body> 下面是我的家庭控制器 jobPortalApp.controller('controllerHome',function($scope, $http) { $scope.test=""; $scope.sig
<body ng-app="jobPortalApp" ng-controller="mainController">
<div ui-view></div>
</body>
下面是我的家庭控制器
jobPortalApp.controller('controllerHome',function($scope, $http) {
$scope.test="";
$scope.signup = function() {
console.log($scope.test);
}
});
所需:
我希望在用户单击注册后,在controllerHome中更改test的值
问题:
console.log输出为空,如果我删除
$scope.test=“”代码>然后输出未定义。我希望在我的控制器中更改test的值。由于原型继承的问题,通常不应该直接绑定到$scope
。尝试从子作用域读取值时,如果该值不存在,则最终从父作用域读取。但是一旦你写了,你就写到了子范围。如果您改为绑定到一个对象,它会工作吗
$scope.data = {
test: ""
};
<input type="text" ng-model="data.test">Test</input>
由于原型继承的问题,通常不应该直接绑定到$scope
。尝试从子作用域读取值时,如果该值不存在,则最终从父作用域读取。但是一旦你写了,你就写到了子范围。如果您改为绑定到一个对象,它会工作吗
$scope.data = {
test: ""
};
<input type="text" ng-model="data.test">Test</input>
谢谢,这很有效!我刚刚用我的测试代码测试了这个。谢谢,这很有效!我刚刚用我的测试代码测试了这个。
$scope.data = {
test: ""
};
<input type="text" ng-model="data.test">Test</input>
<input type="text" ng-model="ctrl.test">Test</input>