1

i have started to study Angular just a little time. I am trying to write an example of Custome Directive with TemplateURL. The problem is that in the new custom tag it's not print the result of the template.

To this link you can see the code.
Index.html

<!doctype html>
<html  xmlns="http://www.w3.org/1999/xhtml" lang="it-IT">

<head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello Angular</title>
     <script src="../../angular.min.js"></script>
     <script type="text/javascript" src="script.js"></script>
</head>
    <body ng-app="myApp" ng-controller="controApp as ctrl">


        <table border=2>
            <thead>
                <td>AAA</td>
                <td>BBB</td>
                <td>CCC</td>
            </thead>
            <tbody>
                <lista-clienti lista="ctrl.elencoClienti"></lista-clienti>
            </tbody>
        </table>

    </body>
</html>

script.js

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

app.directive("listaClienti", function() { 
    return { 
      scope: {
        lista : "="
      },
      //template: "<div>missing tpl</div>"
      templateURL: "selectCity.html"
    }; 
  }); 

app.controller('controApp',  function($scope ){

  $scope.elencoClienti  = [ 
        {codiceCliente: "1", ragioneSociale: "Michele Srl", indirizzo_so:"Via delle calende greche" },
        {codiceCliente: "2", ragioneSociale: "Michele Srl", indirizzo_so:"Via delle calende greche" },
    ]; 


});

selectCity.html

<tr ng-repeat="c in lista">
    <td>{{c.codiceCliente}}</td>
    <td>{{c.ragioneSociale}}</td>
    <td>{{c.indirizzo_so}}</td>
</tr>

what could is the problem? Thanks

Directory Structure: 05_Custom_Directive
-- Example_02
----index.html
----script.js
----selectCity.html

9
  • 2
    Will you paste the directory structure of your application, my hunch is your selectCity.html is not being found by the directive. Commented Apr 11, 2016 at 13:26
  • i have add the directory structure ;) Commented Apr 11, 2016 at 13:46
  • can you change your selectCity.html to be the below <tr ng-repeat="c in lista"> <td>{{c.codiceCliente}}</td> <td>{{c.ragioneSociale}}</td> <td>{{c.indirizzo_so}}</td> </tr> this i a test This will tell us if your template is getting injected or not. Commented Apr 11, 2016 at 13:51
  • nothing, don't work...I also think it is a problem of injection Commented Apr 11, 2016 at 13:54
  • did you try a relative url ./selectCity.html Commented Apr 11, 2016 at 13:57

1 Answer 1

2

When using ctrlAs syntax use this instead of $scope

 this.elencoClienti  = [ 
    {codiceCliente: "1", ragioneSociale: "Michele Srl", indirizzo_so:"Via delle calende greche" },
    {codiceCliente: "2", ragioneSociale: "Michele Srl", indirizzo_so:"Via delle calende greche" },
]; 
Sign up to request clarification or add additional context in comments.

2 Comments

docs.angularjs.org/api/ng/directive/ngController, this is the url that suggests to bind your properties to the object instead of the $scope when you are using the as clause in ng-controller.
ah perfect i don't know this alternative

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.