0

So due to restrictions I have at work I am getting some pretty long includes. To avoid this I have tried creating the following directive:

app.directive('viewInclude', [function() {
        var baseUrl = 'fams360frontend/views/default/glap';
        return {
            scope: {
                viewInclude: '@'
            },
            template: '<div ng-include="link"></div>',
            link: function($scope) {
              $scope.link = baseUrl + $scope.viewInclude;                  
            }
        };
    }]);

I then call it like this:

<view-include src="'/partials/ientry-header.html'"></view-include>      

I am pretty new to Angular so this may be a simple issue but I can't seem to figure out where I am going wrong. I get this error on render:

Error: [$parse:syntax] <!-- ngInclude: fams360frontend/views/default/glap{{viewInclude}} -->

EDIT:

I have updated my code using the answer below but I now no longer get the bank bindings...any ideas?

The included file:

<div style="display: inline-block;">
    <div style="display: inline-block;">
        <span>Bank Account:</span>
    </div>
    <div style="display: inline-block; margin-left: 10px;">
        <span>{{bank.bank_number}} - {{bank.account_name}}</span>
    </div>
</div>
<div style="display: inline-block; margin-left: 75px;">
    <div style="display: inline-block;">
        <span>Company:</span>
    </div>
    <div style="display: inline-block; margin-left: 10px;">
        <span>{{bank.company_number}} - {{bank.company_name}}</span>
    </div>
</div>
4
  • Is there a reason you don't just concatenate scope.viewInclude with the baseUrl? Commented Sep 17, 2015 at 15:41
  • Your edit should really be a second question since it sounds like the answer below helped get you past your original issue. Commented Sep 17, 2015 at 15:56
  • Yes and no since what good is it for me to now be able to bring in a template if all the bindings inside break. Its more that I didn't clarify enough in my original question imo. Now I am clarifying....the include will need to keep any bindings in it valid. Commented Sep 17, 2015 at 16:01
  • its definitely a fine line but I feel that many people will have bindings in their templates and if you can just throw it in the answer then it'll save everyone the headache of searching through multiple questions Commented Sep 17, 2015 at 16:02

2 Answers 2

2

Add a link function and concatenate.

app.directive('viewInclude', [function() {
    var baseUrl = 'fams360frontend/views/default/glap';
    return {
        replace: true,
        scope: {
            viewInclude: '@'
        },
        template: '<div ng-include="link"></div>',
        link: function($scope) {
          $scope.link = baseUrl + $scope.viewInclude;                  
        }
    };
}]);

Additionally. I believe your html needs to be.

<div view-include="asdf"></div> <!-- view-include should be an attribute. And since you're using `@` you don't need to wrap the string in single quotes -->
Sign up to request clarification or add additional context in comments.

4 Comments

says $scope is not defined
Also of note, replace is deprecated so you will not what to use that option if at all possible.
first off link did not need curly braces. Second, please see my edit to see new issue
I missed the note in the docs that ngInclude could take an expression along with a string.
0

Based on comment about losing bindings that is due to the isolated scope you are creating.

A simpler approach would be to not use isolated scope or even ng-include(which creates a child scope) and just use the templateUrl: fn() of directive:

directive('viewInclude', function() {
 var baseUrl = 'fams360frontend/views/default/glap';
  return {
    templateUrl: function(elem, attr){
      return baseUrl + attr.src;
    }
  };
});

Scope of directive will be whatever the scope of parent is wherever it is used this way. Directive is only being used to define source path of template

NOTE: remove extra quotes in your src shown in question

<view-include src="/partials/ientry-header.html"></view-include>

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.