我是一个有角度的新手,我正在尝试一些我认为应该非常简单的东西......但事实证明我并没有把它搞清楚。
我有一个$scope
变量,我希望将其双重绑定(使用ng-model
)到textarea。我能够在js小提琴网站上工作,但现在我的代码。我试图将所有内容剥离到几行但它仍然无效,控制器永远不会更新。
这是我的代码:
JS / main.js
var app=angular
.module('noclu', ['ionic', 'app.controllers'])
.config(function ($stateProvider, $urlRouterProvider){
$stateProvider
.state('menu.main', {
url: "/main",
views: {
'menuContent': {
templateUrl: 'templates/main.html',
controller: 'MainCtrl'
}
}
});
$urlRouterProvider.otherwise("/menu/main");
});
JS / controller.js
angular
.module('app.controllers', [])
.controller('MainCtrl', function ($scope){
$scope.src='---';
$scope.get_feeds=function(){
//seems like that here "this" is actually the textarea ??
//$scope.src is always whatever has been set in the controller
console.log('this:'+this.src); //this output whatever I enter in the textarea
console.log('scope:'+$scope.src); //this always output '---'
};
});
的index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<link rel="icon" type="image/png" href="favicon.png">
<title>NoClu</title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/main.js"></script>
<script src="js/controllers.js"></script>
</head>
<body ng-app="noclu">
<ion-nav-view></ion-nav-view>
</body>
</html>
模板/ main.html中
<ion-view view-title="NoClu" >
<ion-content class="padding" id="src-wrapper-center">
<div ng-class="vertical_center">
<div id="src-wrapper">
<div>
<div class="padding src-title">What are you in the mood for ?</div>
<div class="item" id="src-txt-wrapper">
<textarea id="src" ng-model="src"></textarea>
</div>
<button id="search" class="button button-block button-large button-balanced" ng-click="get_feeds()" >
Let's eat
</button>
</div>
</div>
</div>
</ion-content>
</ion-view>
更新 - 我做到了,但为什么?
我将$scope.src='---';
更改为$scope.src={body:'---'};
,然后将ng-modal
更改为src.body
,从而使其成功。但是..为什么它不起作用,因为它适用于布尔值?
答案 0 :(得分:3)
直接使用$ scope。在angularJS中不是一个好习惯。它有各种帖子,更多关注范围继承。 例如:http://learnwebtutorials.com/why-ng-model-value-should-contain-a-dot
因此,您需要更改模型:
$scope.myModel = {};
$scope.myModel.src = "---"
你的html绑定到myModel.src