我正在研究离子项目。我不知道为什么,但是ng-model
无法正常工作。我已经启动了一个名为tabs的默认starter
的新项目
当我打开浏览器时,我在输入字段中看到“我的自定义文本”,但是当我更改它时,在控制台中我看到相同的文本“我的自定义文本”,但应该使用我的输入进行更改。因此,$scope.getUserName
不会应用更改。我尝试调试并发现它在$scope
的{{1}}内创建了自己的$scope
,但是当我试图像DashCtrl
那样访问它时,我得到{{} 1}}。
任何人都可以解释我发生了什么,我做错了什么?
以下是$scope.child
undefined
这是我的www/js/controllers.js
angular.module('starter.controllers', [])
.controller('DashCtrl', function($scope) {
$scope.getUserName = 'my custom text';
$scope.nameChange = function(){
console.log($scope.getUserName);
};
})
.controller('ChatsCtrl', function($scope, Chats) {
// With the new view caching in Ionic, Controllers are only called
// when they are recreated or on app start, instead of every page change.
// To listen for when this page is active (for example, to refresh data),
// listen for the $ionicView.enter event:
//
//$scope.$on('$ionicView.enter', function(e) {
//});
$scope.chats = Chats.all();
$scope.remove = function(chat) {
Chats.remove(chat);
};
})
.controller('ChatDetailCtrl', function($scope, $stateParams, Chats) {
$scope.chat = Chats.get($stateParams.chatId);
})
.controller('AccountCtrl', function($scope) {
$scope.settings = {
enableFriends: true
};
});
答案 0 :(得分:0)
嘿伙计们,我只知道问题出在哪里。
我将ng-model="getUserName"
替换为ng-model =“userObject.username”,并在/controllers.js
现在它看起来像这样
.controller('DashCtrl', function($scope) {
$scope.userObject = {
username: 'custom'
}
$scope.nameChange = function(){
console.log($scope.userObject.username);
};
})
要了解为什么现在一切正常,请查看本文。
https://github.com/angular/angular.js/wiki/Understanding-Scopes