Angular / ionic - model doesn't update $ scope in controller

I'm new to angularjs and I'm trying something that I believe should be very simple ... but it turns out I don't understand.

I have a variable $scope

that I want to double binding (using ng-model

) to a text area. I was able to get it to work on the js fiddle sites, but now on my code. I tried to expand everything down to a few lines and it still doesn't work, the controller never gets 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 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 eat
                    </button>
                </div>
            </div>
        </div>
    </ion-content>
</ion-view>

      

UPDATE - I got it working, but why?

I got it working by changing $scope.src='---';

to $scope.src={body:'---'};

and then changing ng-modal

to src.body

. but .. WHY didn't it work differently, since it works for boolean?

0


source to share


1 answer


Using $ scope directly. not good practice in angularJS. There are different positions, it is more appropriate to inherit from $ scope. For example: http://learnwebtutorials.com/why-ng-model-value-should-contain-a-dot

Therefore, you need to change your model like this:



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

      

And your html to bind to myModel.src

+3


source







All Articles