0

I'm pretty new to AngularJs, i'm trying to bring on my index a partial, a pretty basic login form with semantic validation.

The issue is that, if i load everything within the index, everything works fine, but if i use Angular routes, my stylesheet and my js script stops working, but only those that have to work with the partials.

Here are the codes.

WORKING CODES: index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset = "UTF-8">

        <!--Mandatory links for styles and scripts-->
        <link rel = "stylesheet" type = "text/css" href = "external/semantic/semantic.css">

        <script type = "text/javascript" src = "external/js/jquery.min.js"></script>
        <script type = "text/javascript" src = "external/semantic/semantic.js"></script>

        <script type = "text/javascript" src = "external/angular-1.5.6/angular.js"></script>
        <script type = "text/javascript" src = "external/angular-1.5.6/angular-route.js"></script>

        <!--Own scripts and styles-->
        <link rel = "stylesheet" type = "text/css" href = "app/styles/main.css">

        <script type = "text/javascript" src = "app/modules.js"></script>
        <script type = "text/javascript" src = "app/controllers/loginController.js"></script>
        <script type = "text/javascript" src = "app/validations/loginFormValidation.js"></script>
    </head>
    <body data-ng-app = "loginApp">
        <div class = "ui middle aligned center aligned grid" data-ng-app = "loginController">
            <div class = "column">
                <h2 class = "ui icon header">
                  <i class = "settings icon"></i>
                  <div class = "content">
                    Inicia sesión.
                  </div>
                </h2>
                <form class = "ui form segment">
                    <div class = "field">
                        <div class = "ui left icon input">
                            <i class = "user icon"></i>
                            <input type = "text" name = "username" data-validate = "username" placeholder = "Nombre de usuario" data-ng-model = "username">
                        </div>
                    </div>
                    <div class = "field">
                        <div class = "ui left icon input">
                            <i class = "lock icon"></i>
                            <input type = "password" name = "password" data-validate = "password" placeholder = "Contraseña" data-ng-model = "password">
                        </div>
                    </div>
                    <div class = "ui error message"></div>
                    <button type = "submit" class = "ui teal fluid submit button" ng-click = "loginUser()">
                        Login
                    </button>
                    <div class = "ui divider"></div>
                    <a href="#">Olvidaste la contraseña?</a>
                </form>
            </div>
        </div>
    </body>
</html>

modules.js

var login = angular.module('loginApp', ['ngRoute']);

loginController.js

login.controller('loginController', function($scope) {
    $scope.loginUser = function (username, password) {
         console.log($scope.username);
        console.log($scope.password);
    }
});

loginFormValidation.js

$(document).ready(function() {
    $('.ui.form')
      .form({
        fields: {
            username: {
                identifier: 'username',
                rules: [{
                    type: 'empty',
                    prompt: 'Ingresa tu usuario.'
                }]
            },
            password: {
                identifier: 'password',
                rules: [{
                    type: 'empty',
                    prompt: 'Ingresa la contraseña.'
                }]
            }
        }
    });
});

Everything up to this point works fine. But I wanted to try out routes adding my stylesheet and the validation script on the partial with no success.

I also tried to leave this files on the index but it didn't work either

BROKEN CODES: index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset = "UTF-8">

        <!--Mandatory links for styles and scripts-->
        <link rel = "stylesheet" type = "text/css" href = "external/semantic/semantic.css">

        <script type = "text/javascript" src = "external/js/jquery.min.js"></script>
        <script type = "text/javascript" src = "external/semantic/semantic.js"></script>

        <script type = "text/javascript" src = "external/angular-1.5.6/angular.js"></script>
        <script type = "text/javascript" src = "external/angular-1.5.6/angular-route.js"></script>

        <!--Own scripts and styles-->
        <link rel = "stylesheet" type = "text/css" href = "app/styles/main.css">

        <script type = "text/javascript" src = "app/modules.js"></script>
        <script type = "text/javascript" src = "app/controllers/loginController.js"></script>
        <script type = "text/javascript" src = "app/validations/loginFormValidation.js"></script>
        <script type = "text/javascript" src = "app/configs/mainRoutes.js"></script>
    </head>
    <body data-ng-app = "loginApp">
        <div data-ng-view></div>
    </body>
</html>

mainRoutes.js

login.config(function ($routeProvider) {
    $routeProvider
        .when('/', {
            controller: 'loginController',
            templateUrl: 'app/partials/login/loginFormView.html'
        })
        .otherwise({
            redirectTo: '/'
        });
});

loginFormView.html

<div class = "ui middle aligned center aligned grid">
    <div class = "column">
        <h2 class = "ui icon header">
          <i class = "settings icon"></i>
          <div class = "content">
            Inicia sesión.
          </div>
        </h2>
        <form class = "ui form segment">
            <div class = "field">
                <div class = "ui left icon input">
                    <i class = "user icon"></i>
                    <input type = "text" name = "username" data-validate = "username" placeholder = "Nombre de usuario" data-ng-model = "username">
                </div>
            </div>
            <div class = "field">
                <div class = "ui left icon input">
                    <i class = "lock icon"></i>
                    <input type = "password" name = "password" data-validate = "password" placeholder = "Contraseña" data-ng-model = "password">
                </div>
            </div>
            <div class = "ui error message"></div>
            <button type = "submit" class = "ui teal fluid submit button" ng-click = "loginUser()">
                Login
            </button>
            <div class = "ui divider"></div>
            <a href="#">Olvidaste la contraseña?</a>
        </form>
    </div>
</div>

It seems to load the form and then semantic gives the style based on the class of the divs, but then it ignores the extra styling applied on "main.css" and the validation on "loginFormValidation.js".

All the other files still work except this two, and i think that this will happen with other files if i add some more.

I don't know what to do now, i tried adding this files into the partial, but it didn't work.

Is there anyway to achieve this?

Thank you very much!!

Fer VT
  • 500
  • 1
  • 8
  • 25
  • CSS should all work...problem is likely related to any javascript enhanced widget/element that semantic would change on initial pageload automatically. You have to use angular directives that initialize those when those elements actually exist – charlietfl Jul 06 '16 at 03:40
  • there are few solutions to this with ngRoute.. but my advice to u is to use ui-router it is more advance and u can reach ur result very easy ,, and by the way it is not hard it is pretty similar to .config in ngRoute. i have asked a similar question like ur question before and and was advised to use ui-router now i tried both and i highly recommend u to try changing very few things to use ui-router.. happy coding. if u r insisting to stay with ngRoute can u post ur css files here too!? – shireef khatab Jul 27 '16 at 23:16
  • https://stackoverflow.com/questions/37096576/angularjs-routing-between-css-files – shireef khatab Jul 27 '16 at 23:22

1 Answers1

0

For starters, have a single 'global' CSS that contains all the rules in all of your CSS files. CSS files will be cached so you'll be saving download time. Just put a reference to that global CSS file in the .

-There are tools out there to load specific CSS per route:

How to include view/partial specific styling in AngularJS

https://github.com/castillo-io/angular-css

The above might be of help to you.

And i highly recommend u to try changing very few things to use ui-router.. happy coding.

Community
  • 1
  • 1
shireef khatab
  • 977
  • 2
  • 13
  • 33