Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS directive not showing up on template

I've got a tiny problem with an angular directive that's now working and I don't know why. I think it's a fairly simple issue that I'm overlooking, maybe you can help me out.

Directive is defined like this:

angular.module('directives', [])
    .directive('my-directive', function () {
        return {
            restrict: 'AE',
            scope: {
                name: '=name'
            },
            template: '<h1>{{name}}</h1>'
        };
    });

Then index.cshtml:

<my-directive name="test"></my-directive>

Application.js:

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

And here's controllers.js

angular.module('controllers', ['apiServices', 'directives'])
    .controller('homecontroller', function($scope, $resource, webApiService, $log, $translate, $localStorage, $sessionStorage) {

Ok confirmed that directives.js is loaded, otherwise application.js nags about 'unknown module'. There are no error messages in the console, the thing just doesn't show. Any ideas?


EDIT

So as pointed out, I changed the directive name to camelCase, but still no luck:

<my-directive name="John Doe"></my-directive>

And

.directive('myDirective', function () {

But nothing is showing yet.

EDIT

Problem is that angular expects an object to be passed into the attribute, not a string literal. If you create an object person = { name: 'John' }, pass the person in, then write {{ person.name }} ( assuming we named the attribute person + scope var person too ).

like image 324
Jochen van Wylick Avatar asked Nov 22 '13 11:11

Jochen van Wylick


People also ask

Can a directive have a template in Angular?

Components are directives with templates. The only difference between Components and the other two types of directives is the Template. Attribute and Structural Directives don't have Templates. So, we can say that the Component is a cleaner version of the Directive with a template, which is easier to use.

How to use directives in AngularJS?

AngularJS directives are extended HTML attributes with the prefix ng- . The ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.

Which directive can have a template?

The component directive is just a directive that attaches the template and style for the element, along with the specific behavior.

What are the directives with a template in Angular called?

Structural directives are directives which change the DOM layout by adding and removing DOM elements. Angular provides a set of built-in structural directives (such as NgIf , NgForOf , NgSwitch and others) which are commonly used in all Angular projects.


2 Answers

During normalization, Angular converts - delimited name to camelCase.

So use camelCase while specifying the directive inside JS:

.directive('myDirective', function () {

Fiddle

like image 114
AlwaysALearner Avatar answered Sep 27 '22 01:09

AlwaysALearner


I'm sure you've figured this out already, but if you change your scope definition for name to be

scope: {
  name: '@'
}

you will then be able to pass a string. The '@' interpolates the attribute while the '=' binds it. Additionally, you don't need to include an attribute name if it is the same as the scope variable.

like image 26
Gene Avatar answered Sep 23 '22 01:09

Gene