4

I have a simple example where I want to show the word "Peter" by directive templating. When a person makes a click on div - the name changes to "Juliet". However I am finding no error and nothing visible on screen.

<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js" ></script> 
</head>
<body ng-app="myApp" ng-controller="myCtrl"> 
    <people name="peter"></people>
    <people name="peter"></people>
    <script>
        //app declaration
        var app = angular.module('myApp', []);
        //controller declaration
        app.controller('myCtrl', function($scope) {
          //code goes here ... 
        });
        //directive declaration
        app.directive('people', function() {
          return {
            restrict: 'E',
            scope: {name:'='},
            template: '<div ng-click="name = \'Juliet\'">{{name}}</div>',
          }
        });
    </script> 
</body> 
</html>
Deadpool
  • 7,811
  • 9
  • 44
  • 88

3 Answers3

3

This is happening because angular evaluate "peter" as an expression, i.e, a variable name that he can't resolve. if you want to pass a string, just put '' around it

Kobi Cohen
  • 678
  • 4
  • 9
1

Yep I found out a bit. Actually,

1) first, I should have used '@' instead of '=', as I wanted the directives to be separated from each other and on click of one should not have effected the value shown by another directive.

2) Second, I should have used model (here $scope.fname) to show the values in directives instead of strings. (as said by Kobi Chen)

2) Thirdly, I should have wrapped the fname in expressions in view in mustache tags.

The complete code is as given:

<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js" ></script> 
</head>
<body ng-app="myApp" ng-controller="myCtrl"> 
    <people name="{{fname}}"></people>
    <people name="{{fname}}"></people>
    <script>
        //app declaration
        var app = angular.module('myApp', []);
        //controller declaration
        app.controller('myCtrl', function($scope) {
          $scope.fname = "Peter";
        });
        //5 directive declaration
        app.directive('people', function() {
          return {
            restrict: 'E',
            scope: {name:'@'},
            template: '<div ng-click="name = \'Juliet\'">{{name}}</div>',
           }
        });
    </script> 
</body> 
</html>

Also, took help from here:

What is the difference between '@' and '=' in directive scope in AngularJS?

Community
  • 1
  • 1
Deadpool
  • 7,811
  • 9
  • 44
  • 88
0

Because peter is a variable if you use scope with "=", so you have to set some values to that variable inside your controller like

app.controller('myCtrl', function($scope) {
  //code goes here ... 
  $scope.peter = 'John';
});

Then you will see something.Peter variable had not value that's why you didn't see anything and couldn't click on it to change to 'Juliet'

Hazarapet Tunanyan
  • 2,809
  • 26
  • 30