1

I have a directive declared inside another directive, which should use a single variable from parent scope. This is what the structure looks like:

MAIN CONTROLLER:

angular.module('app')
    .controller('MainCtrl', function ($scope, Settings){
$scope.userName = "";
$scope.init = function(){
 Settings.getUser()
                .then(function (res) {
                    $scope.userName = res.data.userName;
                    console.dir(res);
                }).catch(function (err) {
                    //showError(err.data)
                });
};
}

main.html

<div id="wrapper">

        <!-- Navigation -->

        <header></header>
        <!-- /.navbar-top-links -->


        <!-- /.navbar-static-side -->

            <div id="page-wrapper" style="min-height: 561px;">

                <div ui-view></div>

            </div>
            <!-- /#page-wrapper -->
    </div>

header.html

<nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
<header-notification user-name="{{userName}}"></header-notification>
</nav>

header-notification.js

angular.module('app')
    .directive('headerNotification', function (Settings, $ngBootbox) {
        return {
            templateUrl: 'scripts/directives/header/header-notification/header-notification.html',
            restrict: 'E',
            scope: {
                'userName': '='
            },
            controller: ['$scope', function ($scope) {
                console.log($scope.userName);
            }],
            link: function (scope, element, attrs) {
                console.log(scope.userName);
            }
        }
    });

I am not able to access the value, I have also tried scope.$watch but still getting undefined in value..

1 Answer 1

1

There are 3 changes you will need to make

  1. Angular JS is not very good with camelcase. For some reason it inconsistently does not recognize/bind them. So, I would use "username"
  2. remove the brackets when passing the variable. So, instead of {{username}} use just username
  3. the variable name (userName - please change this to "username" as well) you have used in directive does not match what you have in the header.html (user-Name)

Hope this solves the problem.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.