2

I'm facing a problem with an undefined function in AngularJs and I didn't find any fitting solution on the internet: I try to build a single page website. A few tabs are nested in the index.html with ng-include.

If i open the tab1.html allone the angular-functions work as expected. But if i open the index.html (with the embedded tab1.html) the function is undefined and the angular function dosn't work anaymore.

I would be very thankful if someone could help me!!!

-code below-

index.html

<!--Tab-Container-->
<div lang="en" ng-app="IndexPage" ng-cloak class="tabsdemoDynamicHeight" ng-cloak="">
<md-content>
    <md-tabs md-dynamic-height="" md-border-bottom="">

        <!-- Content TAB 1 -->
          <md-tab label="Tab1">
            <md-content class="md-padding">
                <div ng-include src="'views/tab1.html'"></div>
            </md-content>
          </md-tab>           

        <!-- Content TAB 2 --> 
        ...

      </md-tabs>      
</md-content>
</div>
<script type="text/javascript">angular.module('IndexPage', ['ngMaterial']);</script>

index.js

angular.module('IndexPage',['ngMaterial', 'ngMessages', 'material.svgAssetsCache']);

tab1.html

<body>
<div ng-app="tableapp" ng-controller="Ctrl" >
            <a href="#" editable-text="user.name">{{ user.name || "empty" }}</a>
        </div>
</body>

tab1.js

var tableapp = angular.module("tableapp", ["xeditable"]);

tableapp.run(function(editableOptions) {
  editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
});

tableapp.controller('Ctrl', function($scope) {
  $scope.user = {
    name: 'awesome user'
  };  
});
5
  • You are not telling in index.js to include the tableapp module, try adding it as a dependency Commented Mar 21, 2016 at 8:32
  • Why are you defining IndexPage twice? (index.js and index.html) Commented Mar 21, 2016 at 9:01
  • remove the script tag from index.html and replace it with script include tag.. to include index.js ...would be first thing to do .. Commented Mar 21, 2016 at 9:19
  • Also you are creating 2 seperate modules and app (viz. tableapp & IndexPage) .. no need to do it as well . Just include the tabs module in your main app.. Commented Mar 21, 2016 at 9:22
  • Thanks for your fast help. I merged the index.js and tab1.js; I removed the redundant IndexPage definition but the error is still the same. How do I exactly add a dependency to the IndexPage module? Commented Mar 21, 2016 at 11:01

1 Answer 1

1

You can't define an ng-app inside an ng-app, and you definitely can't have multiple <body> tags on the same html page, and this is exacltly what happends when angular injects tab1.html template into your index.html.

If tab1.html is just a view inside your main app, it can't be in the form of

<body>
   <div ng-app="tableapp" ng-controller="Ctrl" >
            <a href="#" editable-text="user.name">{{ user.name || "empty" }}</a>
   </div>
</body>

But more like:

<div>
   <div ng-controller="Ctrl" >
            <a href="#" editable-text="user.name">{{ user.name || "empty" }}</a>
   </div>
</div>

And of course inject the tableapp module to your main IndexPage module.

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

1 Comment

Thank you very much, this helped me alot. Now my code is working!

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.