0

I've made a login directive that looks like this:

directive login

  (function() {
        'use strict';

        angular
            .module('lnjapp.login',[])
            .directive('login', login);

        function login() {
            var directive = {
                templateUrl: '/app/components/login/login.html',
                restrict: 'E',
                Controller: login.controller,
                controllerAs: 'vm'
            };
            return directive;
        }   
    })();

login.controller

(function() {
    'use strict';

    angular
        .module('lnjapp.login')
        .controller('loginController', loginController);

    function loginController()
    {
        var vm = this;
        vm.test = 'test';
    }
})();

login.html

        <div class="form-group pull-left">
            <label>
                <b>d{{ vm.test }}</b>
                <input type="checkbox" name="remember">&nbsp;&nbsp;&nbsp;&nbsp;Onhoud mij
            </label>
            <br>
            <a ng-href="#/wachtwoord/vergeten">Wachtwoord Vergeten?</a>
        </div>

In login.html vm.test is empty?

What could be wrong here?

1
  • swap login.controller for 'loginController' Commented May 23, 2016 at 14:46

3 Answers 3

1

There are two issues here: 1. Controller name is incorrect in the directive 2. Controller name should be in single quotes

Try below code:

(function() {
        'use strict';

        angular
            .module('lnjapp.login',[])
            .directive('login', login)
            .controller('loginController', loginController);

            function loginController()
            {
              var vm = this;
              vm.test = 'test';
            };

        function login() {
            var directive = {
                templateUrl: '/app/components/login/login.html',
                restrict: 'E',
                controller: 'loginController',
                controllerAs: 'vm'
            };
            return directive;
        }   
    })();
Sign up to request clarification or add additional context in comments.

4 Comments

Thankyou that's working. But when I put the controller in a separated file it's not? The controller is linked in my index.html
Yes this can be happened and the reason is, when you create controller is a separate file since you are creating 'lnjapp.login' module in a separate file so there may be the case that 'lnjapp.login' module is not available and we are creating controller over it. So if you have decided to put controllers and services in the different file then you will have to make sure the before adding controllers/services over it, module is created and ready to use.
Is it a bad practice to put it in the same file?
yes if you have too many modules in your project then you should not be using single file. You can use RequireJS/CoomonJS to modularize your application and dependencies.
0

in the directive in should be a variable name not a file name

  (function() {
    'use strict';

    angular
        .module('lnjapp.login',[])
        .directive('login', login);
    function loginController()
    {
       var vm = this;
       vm.test = 'test';
    }
    function login() {
        var directive = {
            templateUrl: '/app/components/login/login.html',
            restrict: 'E',
            Controller: loginController,
            controllerAs: 'vm'
        };
        return directive;
    }   
})();

Comments

0

In order to access controller data from the view, you should bind it to the scope. So in your case you should user $scope.vm = "test"; and in the view only use {{vm}} that should do the trick as far as I can see.

For further information about AngularJS's two way data binding see: https://docs.angularjs.org/tutorial/step_04

Comments

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.