【问题标题】:angular/ionic - model does not update $scope in controllerangular/ionic - 模型不更新控制器中的 $scope
【发布时间】:2015-06-15 16:13:32
【问题描述】:

我是 angularjs 的新手,我正在尝试一些我认为应该非常简单的东西......但结果我没有弄清楚。

我有一个$scope 变量,我想将它双重绑定(使用ng-model)到一个文本区域。我能够让它在 js fiddle 网站上工作,但现在在我的代码上。我试图将所有内容精简到几行,但仍然无法正常工作,控制器永远不会更新。

这是我的代码:

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 使其工作。但是..为什么不能以其他方式工作,因为它适用于布尔值?

【问题讨论】:

  • 查看我的回复以了解“为什么”

标签: javascript angularjs data-binding ionic-framework


【解决方案1】:

直接使用 $scope。在 angularJS 中不是一个好习惯。它有各种各样的帖子,更关注 $scope 继承。 例如:http://learnwebtutorials.com/why-ng-model-value-should-contain-a-dot

因此,您需要像这样更改模型:

$scope.myModel = {};
$scope.myModel.src = "---"

还有你要绑定到myModel.src的html

【讨论】:

  • 太好了,我从文章中了解到The key here is that ng-switch is a directive that creates a new scope. There are other directives that does this.。所以我可能在不知道的情况下使用了一个创建新范围的指令。必须找到那些是什么。感谢您的帮助
  • 当本地 $scope 变得太大时,Angular 似乎会出现问题。在$scope 中创建对象更干净,并且消除了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多