Problems when it detects the functions of my controller

0

I am trying to make a Menu (with first, second and dessert) and I would like to enter a input to enter a new dish in the array I created (to show it with ng-repeat ).

It seems that I do not fit into the functions that I believe in the controler and it does not do anything to me. He does not show me any error ...

I'm running out of ideas. I leave the HTML code and the Angular.js.

  

HTML code

<!DOCTYPE html>
<html>
<head>
    <title>Angular JS</title>
</head>
<!-- Angular JS -->

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>


<body ng-app="myApp">
     <div  ng-controller="MenuController">
        <ul>

            <h1>Menu</h1>
            <h2>Primer Plato</h2>

            <li ng-repeat="x in menu.primero">
                    {{ x }}

            </li>
            <input type="text" ng-model="cambiarPrimero" ng-keypress="MenuController.keyPressOnForm($event)" />
            <h2>Segundo Plato</h2>

            <li ng-repeat="x in menu.segundo">
                    {{ x }}
            </li>

            <h2>Postre</h2>

            <li ng-repeat="x in menu.postre">
                    {{ x }}
            </li>
        </ul>
    </div>


    <script type="text/javascript" src="app.js"></script>
    </body>
    </html>
  

AngularJS code (save it in the root as app.js)

var app =  angular.module('myApp', []);

app.controller("MenuController", function ($scope) {
    $scope.menu= {
      primero: ['sopa', 'paella','calamares'],
      segundo: ['carne','pescado','tortilla'],
      postre: ['fruta','yogur']
    };



    var keyPressOnForm = function(event) {
      if (event.keyCode === 13) { //la key 13 es el enter
        function updatePlato(){
            $scope.menu.primero.push($scope.cambiarPrimero.text);
        }
        updatePlato();
      }
    }
});

Every idea is good.

    
asked by PictorGames 17.01.2017 в 19:19
source

1 answer

1

Podrias probra making the following modifications, in your html:

ng-keypress="keyPressOnForm($event)

On your controller:

$scope.keyPressOnForm = function(event) { if (event.keyCode === 13) { //la key 13 es el enter function updatePlato(){ $scope.menu.primero.push($scope.cambiarPrimero); } updatePlato(); } }

By using the $ scope.keyPressOnForm you are reaching the function within your controller.

    
answered by 17.01.2017 / 21:09
source