0

I'm trying to create navigation tree for my application using custom directive for it. For now I have the following in my directive (minified):

(function() {
  'use strict';

  angular
    .module('app.layout')
    .directive('cnNavTree', ['$log', '$state', cnNavTree]);

  function cnNavTree($log, $state) {
      var directive = {
        link: link,
        restrict: 'EA',
        templateUrl: 'app/layout/cn-nav-tree.html'
      };
      return directive;

    function link(scope, element, attrs) {
      scope.$on('stateChange', function (event, toState, fromState) {
        updateNavTree(event, toState, fromState)
      });

      function updateNavTree(event, toState, fromState) {
        element.html('<span class="color" ui-sref="app.twoWays">Report load</span>');
      }
    }
  }
})();

Everything is OK except ui-sref in element.html, it's not working and I cant go to app.twoWays state. Any ideas, how can we solve the problem?

Thanks in advance

8
  • It would be easier to help if you provided a reproducible example Commented Apr 29, 2017 at 18:26
  • '$stateChangeStart' not 'stateChange', and read about $state.get to generate the url from the code Commented Apr 29, 2017 at 18:29
  • @AlonEitan, it's not $rootScope.$on('$stateChangeStart', ..., it's my custom $scope.$broadcast(...) Commented Apr 29, 2017 at 18:32
  • include $rootScope on Directive Commented Apr 29, 2017 at 18:33
  • 3
    You can't manually insert directives without using $compile() so angular is aware of them and actually compiles them Commented Apr 29, 2017 at 18:54

1 Answer 1

1

As there are angular binding available on DOM. You have to compile the HTML element with current scope, before injected that DOM inside DOM tree.

//inject $compile before using it.
var compiledDOM = $compile('<span class="color" ui-sref="app.twoWays">Report load</span>')($scope);
element.append(compiledDOM);
Sign up to request clarification or add additional context in comments.

1 Comment

I'll try it in few minutes

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.