AFS
AFS

Reputation: 1443

Call controller method on angular

This is the fist angular app that I try to make ,let's see

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

app.controller('CochesController', function($scope) {
   $scope.coche ={
    marca:"Renault",
    modelo:"Clio",
  };

  $scope.nombreCompleto = function(){
    var x = $scope.coche;
    return x.marca+" "+x.modelo;
  };
})

and the view

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link href="style.css" rel="stylesheet" />
    <script data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js" data-require="[email protected]"></script>
    <script data-require="[email protected]" data-semver="1.4.8" src="script.js"></script>
    <script src="app.js"></script>
  </head>

  <body ng-app="miApp" ng-controller="CochesController">
    <p>{{coche.nombreCompleto()}}</p>
  </body>

</html>

I can use the attributes marca and modelo but I can't use the function ¿whats is wrong?

Upvotes: 1

Views: 244

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136124

You could directly call the method available inside a controller scope by methodName, It should be like below,

<p>{{nombreCompleto()}}</p>

Upvotes: 1

Related Questions