angular/ionic - model does not update $scope in co

2019-09-04 04:42发布

问题:

I am a newb with angularjs and I am trying something that I believe should be very simple ... but turns out I am not figuring it out.

I have a $scope variable that I want to double bind (using ng-model) to a textarea. I was able to make it work on js fiddle websites but now on my code. I have tried to strip everything down to just a few lines and it still doesn't work, the controller is never updated.

this is my code:

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>

template/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>

UPDATE - I made it work, but why ?

I made it work by changing $scope.src='---'; to $scope.src={body:'---'}; and then changing the ng-modal to src.body. but.. WHY did not work the other way as it works for boolean?

回答1:

Using directly $scope. is not a good practice in angularJS. There are various post of it, more concernign $scope inheritence. For exemple : http://learnwebtutorials.com/why-ng-model-value-should-contain-a-dot

Therefore, your need to change your model like that :

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

And your html to bind to myModel.src