0

I'm new to Angularjs and haven't touched front-end development since Dreamweaver was the go-to IDE. At work, I'm tasked with adding a new screen to an existing web service. Below are the files I'm working with. Since this is all new to me, and the web service is already really robust, I'm trying to take the same approach as my colleagues, so I've cloned the simplest service and cut a lot of it out. Therefore the conventions and such below are not chosen by me, so if you have a better practice I would love to hear about it (especially explanations of things that are happening here that may not be obvious).

Specifically, I'm looking for clarity on what I'm doing wrong with making a custom directive because the template isn't replacing the HTML tag. I think it's because the tutorials I see don't specify which JS code goes where w.r.t. the controller, module, and the service. Furthermore, I'm working on an already routed page, so all of the tutorials dealing directly with app.js and index.html aren't really helpful.

autoDeploy.js Controller:

angular.module("gms.autoDeploy").controller('autoDeployController', function($scope) {
    var model = this;

    init();

    function init() {
        // A definitive place to put everything that needs to run when the controller starts. Avoid
        //  writing any code outside of this function that executes immediately.
        model.isInstantiated = true;

    }

    // This probably belongs in a different .js file.
    function tibCopy($timeout, $state) {
        return {
            restrict: 'E',
            replace: true,
            templateUrl: 'templates/tibcoCopy.tpl.html'
        };
    }
});

autoDeploy.module.js Module:

// why use iife?
(function(module) {
    'use strict';

    module.config(function($stateProvider) {
        $stateProvider.state('autoDeploy', {
            url: '/autoDeploy',
            views: {
                "main": {
                    controller: 'autoDeployController as autoDeploy',
                    templateUrl: 'autoDeploy/autoDeploy.tpl.html'
                }
            },
            data: { pageTitle: 'Automated Deployment' }
        });
    });

}(angular.module("gms.autoDeploy", [
    'ui.router'
])));

autoDeploy.service.js Service:

/*jslint node: true */
'use strict';

(function() { //start iife

    // Copied from simplest module in the web service, I don't know if the variables below will be needed, or what most of them are for.
    angular.module('gms.autoDeploy')
        .factory('autoDeployService', ["$http", "$q", "$log", "$cookies", "APP_CONFIGS", "SweetAlert", "$timeout", "GridSettingsService", "APP_USER", AutoDeployService]);


    // Copied like above, keeping it here because it seems important, like a constructor.
    function AutoDeployService($http, $q, $log, $cookies, APP_CONFIGS, $timeout, SweetAlert, GridSettingsService, APP_USER) {
        //return service object at bottom of function
        var gridStateEnabled = false;
        var l_gridState = { isSet: false };
        var srvDeferred = $q.defer();


        function processData() {
            //nothing here yet, but keeping to show the returned
            // 'service' variable holds a mapping of functions. Maybe it's how they can be called from the DOM?
        }


        var service = {
            processData: processData
        };

        return service;

    } //end autoDeployService
}()); //end iife

autoDeploy.tpl.html page (does this naming indicate that my page is a template? All source files other than index.html have the .tpl extension; are they all templates?):

<h1>Automated Deployment</h1>

<head></head>

<body>
    <br/>
    <hr>
    <h3>TIBCO Promotion</p>
        <!-- Keeping both of these because I don't know which format is correct --> 
        <tib-copy></tib-copy>
        <tib-copy/>
</body>

Inside of a sub-directory (named templates, and at the same directory level as the above files) is the tibCopyDirective.js directive. I think this is the most incorrect part of my operation:

var app = angular.module('tibCopy', []);

app.controller('autoDeployController', function($scope) {
    $scope.name = "world";
});

app.directive('tibCopy', function() {
    return {
        restrict: 'A',
        replace: 'true',
        templateUrl: 'templates/tibcoCopy.tpl.html'
    };
});

And the actual template, tibcoCopy.tpl.html:

<fieldset>
    <legend>Copy</legend>

    <div><input type="text" /></div>
    <br/>
    <div><input type="text" /></div>
    <br/>
    <div><input type="text" /></div>
    <br/>
    <div><input type="text" /></div>
</fieldset>

I just want the above template to show up on the DOM so I can learn how to dynamically generate duplicates via the click of a button.

EDIT:

app_files: {
    js: [ './src/**/*.module.js', 'src/**/*.js', 'src/**/GMS2_0.js',       //include source files
         '!src/**/*.spec.js', '!src/assets/**/*.js', '!src/**/*.e2e.js', '!src/**/*.page.js' ],  //exclude test files
    jsunit: [ 'src/**/*.spec.js' ],     //unit tests for karma
    jse2e: [ 'src/**/*.e2e.js' ],       //end-to-end tests for protractor

    coffee: [ './src/**/*.module.coffee', 'src/**/*.coffee', '!src/**/*.spec.coffee' ],
    coffeeunit: [ 'src/**/*.spec.coffee' ],

    appTemplates: [ 'src/app/**/*.tpl.html' ],
    commonTemplates: [ 'src/common/**/*.tpl.html' ],
    mockData: [ 'src/common/dataMocks/**/*.json' ],

    webLogicData: [ 'enviroment_configs/WEB-INF/*' ],       //used for deploying to weblogic

    toBeCopied: [ 'src/common/layouts/**/*.html',
        'src/app/**/*.html', '!src/app/**/*.tpl.html' ],       //non-cached templates for modals

    html: [ 'src/index.html' ],
    less: ['src/less/main.less'] //Additional less files should be imported from main.less ... go there now, do not add here
},
Darrel Holt
  • 870
  • 1
  • 15
  • 39
  • 1
    what's in your app.js? Does it load the `tibCopyDirective.js` ? – Anthony C Nov 02 '17 at 22:23
  • Well, I didn't write `app.js` so I didn't think it would be safe to post it here in case it exposes proprietary information. I don't see an explicit loading of the file in `app.js`, but `index.html` has this line: `<% scripts.forEach( function ( file ) { %> <% }); %>` which I believe loads all `.js` files in the directory structure. Viewing the live page source shows: `` – Darrel Holt Nov 02 '17 at 22:30
  • @AnthonyC I modified it slightly to what was mentioned in an answer here: https://stackoverflow.com/questions/16656735/insert-directive-programmatically-angular I can get it to inject the chart element like he does. When i attempt to change the `template:` to `templateUrl:`, upon clicking the button I get this: `Error: [$compile:tpload] Failed to load template: /src/app/autoDeploy/tibcoCopy.tpl.html (HTTP status: 404 Not Found)` note: I deleted the templates folder, so the path above should be correct. – Darrel Holt Nov 02 '17 at 22:34
  • 1
    We can easily test if the templateUrl is the issue. In you original example, see what happens if you change `templateUrl: 'templates/tibcoCopy.tpl.html'` to `template: '
    test
    '`
    – Anthony C Nov 02 '17 at 22:46
  • @AnthonyC That works. How should I change the path to the html file for it to pick it up correctly? I've tries everything from `/src/app/autoDeploy/templates/tibcoCopy.tpl.html` to a relative lookup, and even deleting the `templates` folder and dropping the html file in the `autoDeploy` directory. – Darrel Holt Nov 02 '17 at 22:50
  • 1
    Will need to see the build script (grunt/gulp/webpack/etc) know how you compile the template – Anthony C Nov 02 '17 at 22:55
  • @AnthonyC we use Grunt, would the file be `gruntfile.js`? because it's over 1k lines. Maybe you could tell me what I'm looking for? Maybe something specific I can search for? EDIT: see the update in the main question, I think I may've found it. – Darrel Holt Nov 02 '17 at 22:57
  • 1
    I see you posted the configuration for ` appTemplates` &`commonTemplates`. That's helpful. What is the path of `tibcoCopy.tpl.html` from the `src` directory? – Anthony C Nov 02 '17 at 23:08
  • 1
    @AnthonyC I got it, thanks for your help. I looked at my module file which was working correctly, the path needed was `autoDeploy/tibcoCopy.tpl.html` – Darrel Holt Nov 02 '17 at 23:09
  • I just noticed something odd. It wasn't working when the file was in `autoDeploy/templates/`, but did work when the folder was named differently, indicating that it's a reserved keyword or is meant to indicate some sort of special directory. I assume such, because on the left Explorer panel in VS Code, changing the folder name to `template` or 'templates` causes this to break. Maybe a passerby won't mind explaining why the folder being named `template(s)` causes a conflict if this isn't something specific to my `gruntfile.js`. – Darrel Holt Nov 02 '17 at 23:21

0 Answers0