2

I need to create a web app that can handle different template (same class but with addition field/functions). I use AngularJS.

For all the css side, I use SASS. For the HTML part, I use ng-include of AngularJS. But I don't know what to use for the JS part. I could have the same js file for each template with all the changes but if there a change I would need to change all the files. What I look for is a framework/tool that allow me to have a base js file and load inside extra content from other file (like ng-include in AngularJS).

Thanks in advance

1 Answer 1

1

I think that you a looking for ngRoute or ui-router. They are components that let you create routes in you application. It means that you can define one route that contains a HTML file and a Controller associated.

Take a look at the documentation of the two.

This is an example using ngRoute: https://jsfiddle.net/relferreira/t36xa01c/

HTML:

<div data-ng-app="app">

   <ul class="nav navbar-nav" id="subject">
     <li><a href="#/">Main</a></li>
     <li><a href="#/detail">Detail</a></li>
  </ul>
  <div ng-view></div>

</div>

JS:

angular.module('app', ['ngRoute']);

angular.module('app')
    .config(config)
    .controller('MainController', mainController)
  .controller('DetailController', detailController);


config.$inject = ['$routeProvider'];
function config($routeProvider){

    $routeProvider.when('/',{
        template:'<h1>{{mainVm.title}}</h1>',
        controller:  'MainController as mainVm'
    })
    .when('/detail',{
         template:'<h1>{{detailVm.title}}</h1>',
        controller:  'DetailController as detailVm'
    })
    .otherwise({
        redirectTo:"/"
    });
 }

mainController.$inject = ['$scope'];

function mainController($scope){

    var vm = this;

    vm.title = 'Main'

}

detailController.$inject = ['$scope'];

function detailController($scope){

    var vm = this;

    vm.title = 'Detail'

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

3 Comments

Thank you Renan, but it is not what I am looking for. I need a tool to build the app. For example, I have default/js/main.js with classes and I have template1/js/main_add.js So what I want is to merge the data of the main_add.js with main.js. I look to requireJS side but I don't think it could fit with problem.
You want to put all your JS in one file?
Finally I used your answer Renan to provide a system with overriding basics functions. Thank you for the lead !

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.