Angular UI-router not loading my controllers - javascript

So in case it matters, I am using angular in rails. And yes my sprockets is the correct one. So my templates and routes are loading correctly. But my controllers although loading arent being found by ui-router or are being ignored. For a long time i used
ng-controller="mainctrl"
in my app to fix the problem and pushed it aside, but now i need the controller to be loaded in the routes and its not working. So here is my routes file.
app.config(['$stateProvider','$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$stateProvider
// The home routes are at the top of the page.
.state('home', { // This is our navbar, it leads to all of our login screens
url: '/home',
templateUrl: 'home/nav.html',
authenticate: true,
controller: 'AuthCtrl'
})
.state('login', {
url: '/login',
templateUrl: 'home/login.html',
authenticate: false,
contoller: 'AuthCtrl'
// controller: function($scope) {
// $scope.usererror_hide = true;
// $scope.passworderror_hide = true;
// }
})
$urlRouterProvider.otherwise("/login");
Now whats kind of crazy is that if i uncomment that controller:function logic it works. But for some reason it cant find my AuthCtrl. So im not sure if its a rails problem or a routes problem, because they both seem to be functioning.
this is my controller file, though i dont believe its the source of the problem.
var app= angular.module('planoxApp')
app.controller('AuthCtrl',['$http','$scope', '$localStorage', //be careful with the order of http/scope, they get screwey
'$stateParams','$filter','$state','AuthService','$uibModal',
function($http,$scope,$localStorage, $stateParams,$filter,$state,AuthService,$uibModal){
$scope.selectedGlobal = "" //For our future global search, move it to nav
$scope.datas = [] //For global crap, move to nav
//My variable collections that I need access too
$scope.usererror_hide = true;
$scope.passworderror_hide = true;
my application.js file
//=require jquery
//=require jquery_ujs
//=require jquery-ui
//=require jquery-minicolors
//=require tinycolor
//=require bootstrap
//=require angular
//= require angular-route
//=require angular-rails-templates
//require angular-minicolors
//=require angular-animate
//=require angular-bootstrap
//require angular-bootstrap/ui-bootstrap-tpls-0.14.3.js
//=require lodash
//=require angular-resource
//=require holderjs
//=require angular-holderjs
//=require restangular
//=require angular-dragdrop
//=require angular-strap
//=require nya-bootstrap-select
//=require angular-ui-router
//=require angular-ui-router.stateHelper
//=require angular-color-picker
//= require angular-xeditable
//=require angularjs-file-upload
//=require ngstorage
//= require_tree ./templates
// = require_tree .
and my app.js file
planoxApp= angular.module('planoxApp',
['ui.router',
'templates',
'nya.bootstrap.select',
'ngAnimate',
'ui.bootstrap',
'main-directives',
'color.picker',
'xeditable',
'restangular',
'ngDragDrop',
'angularFileUpload',
'ngStorage',
'ngHolder',
'ngResource'
// 'mgcrea.ngStrap',
// 'mgcrea.ngStrap.typeahead',
// 'mgcrea.ngStrap.tooltip',
// 'mgcrea.ngStrap.helpers.parseOptions',
])
Finally the actual html file, which is loading just fine.
<div id="login" >
<div class="container">
<div class="row vertical-offset-100">
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-default">
<div class="panel-body">
<h1> PlanoX</h1>
<h4> A Catchy Line About PlanoX. </h4>
<form accept-charset="UTF-8" role="form">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="E-mail" name="email" type="text" ng-model="login_username">
<p ng-hide="usererror_hide"> This email is not in our system, please try again or contact planomatic <p>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password" value="" ng-model="login_password">
<p ng-hide="passworderror_hide"> This password is not in our system, please try again or contact planomatic <p>
</div>
<div class="checkbox">
<label>
<input name="remember" type="checkbox" value="Remember Me"> Remember Me
</label>
</div>
<input class="btn btn-primary btn-block" type="submit" value="Login" ng-click="CheckUser(login_username, login_password)">
</fieldset>
</form>
<p>© 2015 PlanoX LLC | 888-988-PlanoX (3453) | info#PlanoX.com</p>
</div>
</div>
</div>
</div>
</div>
</div>
If anyone can help me I would really appreciate it, i have been struggling for the past week with this.
-----update-----
So it was just a typo issue, but i wanted to post another problem which i incorrectly though was the same as this one. But turned out to be totally different. This is not the correct way to access a controller in a nested view.
.state('home.index', { // our home page for employee standard
url: '/index',
controller: 'HomeCtrl',
views:{
"":{ templateUrl: 'home/home.html'},
"assembly#home.index":{ templateUrl: 'home/assembly.html'},
"client#home.index":{ templateUrl: 'home/client.html'},
"photoplan#home.index":{ templateUrl: 'home/photoplan.html'}
},
authenticate: true
controller: 'HomeCtrl'
})
This is the correct way to access it.
.state('home.index', { // our home page for employee standard
url: '/index',
controller: 'HomeCtrl',
views:{
"":{ templateUrl: 'home/home.html',
controller: 'HomeCtrl'},
"assembly#home.index":{ templateUrl: 'home/assembly.html'},
"client#home.index":{ templateUrl: 'home/client.html'},
"photoplan#home.index":{ templateUrl: 'home/photoplan.html'}
},
authenticate: true
})
This is just in case in the future anyone sees this question and has a similar problem.

Check the typo when setting the controller:
.state('login', {
url: '/login',
templateUrl: 'home/login.html',
authenticate: false,
controller: 'AuthCtrl'
})
controller

Related

ui-router nested state won't load

This is a pretty standard question, however I can't seem to find a solution in anything online. Below is the router/index.html page I'm using. I'm not getting any errors in the console, however when the url resolves, correctly, to the login page the entire page is blank. Any ideas, looking at the code below, as to what I'm missing?
router
(function(){
'use strict'
var app = angular.module('app.core');
app.config(AppRouter);
AppRouter.$inject = ['$stateProvider', '$urlRouterProvider'];
function AppRouter($stateProvider, $urlRouterProvider){
$urlRouterProvider.otherwise('/login');
$stateProvider
.state('main', {
url: '/main',
abstract: true,
resolve:{
Config: 'Config',
config: function(Config){
console.log(Config);
return Config;
}
}
})
.state('main.login', {
url: '/login',
templateUrl: 'app/components/login/login.html',
controller: 'LoginController',
controllerAs: 'vm',
reloadOnSearch: false
})
}
})();
index.html
<div class="slide-animation-container">
<div ui-view id="ng-view" class="slide-animation"></div>
{{scrollTo}}
</div>
login.html
<div class="container" ui-view>
<div class="row vertical-center-row">
<form id="loginForm" class="form-signin" style="max-width:300px;" autocomplete="off" ng-if="attemptLogin">
<h2 class="form-signin-heading" style="font-size:22px">Please Sign In</h2>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span></span>
<input type="text" class="form-control" id="username" placeholder="User Name" ng-model="user.username" required>
</div>
</form>
</div>
</div>
EDIT
The router above resides in my config.js file for my core module:
core.module.js
(function(){
'use strict'
angular.module('app.core', ['angulartics', 'angulartics.google.analytics', 'angulartics.scroll', 'ngRoute', 'ngAnimate', 'ng.deviceDetector', 'ui.router',
'ui.bootstrap', 'ngTable', 'ngSanitize', 'ngCsv', 'toastr', 'angular.chosen', 'rzModule', 'publicServices']);
})();
Your otherwise logic changes the url to /login when no states match.
$urlRouterProvider.otherwise('/login');
However, your state definitions do not have a url that matches /login.
Because main.login is a child of main, it inherits the URL from main.
Therefore, you should use the full URL for main.login in your otherwise() call.
$urlRouterProvider.otherwise('/main/login');
Your module should have injected ui.router as a dependency ,
var app = angular.module('app',['ui.router']);
DEMO

HTML highlighting PRISM with Angular.js

I have the problem with HTML highlighting by using Prism.js
The problem was exactly the same with Prism HTML highlighter . But the solution there do not work in my case.
As you can see there, the code was not hightlighted
[Source Code]
<link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.4.1/themes/prism.min.css" rel="stylesheet" />
<div class="container" >
<div class="jumbotron" style="text-align: center">
<h1>Lanyang Chat</h1>
<h1><small>Presentation</small></h1>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Testing</h3>
</div>
<div class="panel-body">
<pre><code class="language-markup"><script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.4.1/prism.min.js"></script></code></pre>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.4.1/prism.min.js"></script>
[Update]
There was nothing wrong the code, the problem was on implementation with AngularJS
[Temporary Solution] - by #Angelo
Link : http://webtoutsaint.com/prismjs_eng
I use page controller method and slightly change the code since the code provided in the link gave me some syntax error.
app.controller('PrismCtrl', function () {
console.log("Page Controller reporting for duty.");
Prism.highlightAll();
//Here is the problem, Prism is undefined if I include the js file on partial page, But work perfectly if I include it on index.html
});
The problem was solved when including the prism.js on index.html. But failed with undefined error while including the prism.js on the partial/template page.
*Note: I'm not familiar with Angular.js, anyone can solve me this problem?
main.js
var app = angular.module('LanyangChat', ['ngRoute']);
app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when("/", {templateUrl: "pages/login.html", controller: "PageCtrl"})
.when("/login", {templateUrl: "pages/login.html", controller: "PageCtrl"})
.when("/chat", {templateUrl: "pages/chat.html", controller: "PageCtrl"})
.when("/online", {templateUrl: "pages/online.html", controller: "PageCtrl"})
.when("/presentation", {templateUrl: "pages/presentation.html", controller: "PrismCtrl"})
.when("/timeline", {templateUrl: "pages/timeline.html", controller: "PageCtrl"})
.when("/changelog", {templateUrl: "pages/changelog.html", controller: "PageCtrl"})
.when("/privacy-tos", {templateUrl: "pages/privacy-tos.html", controller: "PageCtrl"})
.when("/about", {templateUrl: "pages/about.html", controller: "PageCtrl"})
.when("/404", {templateUrl: "pages/404.html", controller: "PageCtrl"})
.otherwise({redirectTo: '/404'});
}]);
app.controller('PageCtrl', function (/* $scope, $location, $http */) {
//console.log("Page Controller reporting for duty.");
});
app.controller('PrismCtrl', function () {
console.log("Page Controller reporting for duty.");
Prism.highlightAll();
});
Seems that prism.js and frameworks using AJAX as Angular, do not mix up. Although, it's not impossible.
The following topic displays an example on how to use them together:
http://webtoutsaint.com/prismjs_eng
More helpful topics:
Highlights don't work in external template
http://prismjs.com/faq.html#basic-usage

templateURL in ui-router using express

I generate a express project with ejs views engine.
I'm trying to set a templateURL for my home views, but it always feedback a 404 error.
Here are my codes.
In my public/javascripts/angularApp.js
app.config([
'$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterProvider){
$stateProvider
.state('home',{
url: '/home',
templateUrl: 'home.html',
controller: 'MainCtrl',
resolve:{
postPromise:['posts', function(posts){
return posts.getAll();
}]
}
})
And in views/index.ejs
<div class="row container">
<div class="col-md-6 col-md-offset-3">
<ui-view></ui-view>
</div>
</div>
In views/home.html
<div class="page-header">
<h1>Flapper News</h1>
</div>
Please help me for this problem.
thanks alot~
This is answered in how-should-i-create-the-path-of-templateurl-property-in-angular-ui-router if using express. Then home.html must be inside the public folder, eg:
public
|--app
|--views
|--home.html
and the route would be:
...
templateUrl: '/app/views/home.html',
...

ng-click and ng-submit doesn't work

i have a angular application with phonegap where i have a login form and login controller.
The problem is that the ng-submit doesn't work. Normaly the submit call the fonction formConnexion() but nothing happens. So, i tried with just a alert, but it's the same result...
After i tried with a ng-click and same result. Then, i wanted try to call a sample variable in the scope in the template ($scope.test) and it doesn't display. And the console.log('salut!!') doesn't dispaly when i am on the login page.
Which made me think that my controller it doesn't associate to the template.
But i have the ng-controller in my div with the good name controller.
Then, i thought that angular it's was worse installing but no because the other directives work (ng-if, ng-src, ...)
So, if you have a idea to solve this problem, I do not mind :)
here the code for the template :
login.html
<div class="row jumbotron" style="margin:10px" ng-controller="LoginCtrl">
<div class="col-lg-8 col-lg-offset-2 col-md-8 col-md-offset-2 col-sm-8 col-sm-offset-2 col-xs-8 col-xs-offset-2">
<h1>{{ test }}</h1>
<form class="form-horizontal" ng-submit="alert('alert')">
<div class="form-group">
<label for="login" class="control-label">Login</label>
<input type="text" name="login" class="form-control" ng-model="formData.login">
</div>
<div class="form-group">
<label for="password" class="control-label">Password</label>
<input type="password" name="password" class="form-control" ng-model="formData.password">
</div>
<button type="submit" class="btn btn-md btn-success">Connexion</button>
</form>
click
<img ng-src="images/accueil.png" class="accueil img-responsive">
</div>
</div>
and here the controller :
login.js
'use strict';
appStat.controller('LoginCtrl', function ($scope, $rootScope, $http, $location) {
console.log("salut!!");
$scope.formData = {};
$scope.test = "test";
$scope.toto = function() { alert('alert'); };
/**
* Connect l'user
*/
$scope.formConnexion = function () {...}
});
and here my app.js :
app.js
'use strict';
var appStat = angular.module('posStatsPhoneGap', [
'ngCookies',
'ngResource',
'ngSanitize',
'ngRoute',
'ngResource',
'ui.bootstrap',
'ngMessages',
'ngAnimate',
'ngAria',
'ngTouch',
'picardy.fontawesome'
])
.config(['$routeProvider', '$compileProvider', function ($routeProvider, $compileProvider) {
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|file|tel):/);
$compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|file|tel):/);
$routeProvider
.when('/', {
templateUrl: 'views/login.html',
controller: 'LoginCtrl'
})
.when('/main', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/stat', {
templateUrl: 'views/stat.html',
controller: 'StatCtrl'
})
.otherwise({
redirectTo: '/'
});
}
]);
Thank you in advance !
Following from my comment Try something like this:
Create a Controller file & a controller within it Create a angular
module within the controller file
Create a controller.js file then create a angular module see code below:
//Within your controller.js file
var myApp = angular.module('myApp', []);
myApp.controller('myController', function($scope, $http){
$scope.Data = "Alert Alert";
// Create a function within the scope of the module
$scope.myFunc = function(){
alert($scope.Data)
};
});
Then within your HTML file call the function on-Click see code below:
Note: Don't forget to add the controller you created into your HTML code. I usually add it in the body tag
<body ng-controller="myController">
<button ng-click="myFunc()">Make Request Button</button>
</body>
See a live example here:
http://plnkr.co/edit/sQ0z7TlyWv5fM5XyfujK?p=preview
Also a note: ng-click is mostly used for any click events you are trying to perform within your angular app but ng-submit is mostly used when working with a HTML form submission.
Use ng-submit in you form as:
<form ng-submit="submit()">
function Ctrl($scope) {
$scope.list = [];
$scope.text = 'hello';
$scope.submit = function () {
if ($scope.text) {
$scope.list.push($scope.text);
$scope.text = '';
}
};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<div ng-controller="Ctrl">
<form ng-submit="submit()">Enter text and hit enter:
<input type="text" ng-model="text" name="text" />
<input type="submit" id="submit" value="Submit" /> <pre>list={{list}}</pre>
</form>
<button ng-click="submit()">Submit 2</button>
</div>
</div>
I found the error. It was caused by having 2 controllers with the name 'LoginCtrl'. It was a stupid copy/paste error.
I have created one working code for the above question for 4 different type of HTML element, for complete code please follow the below URL -
http://plnkr.co/edit/Tm2Rtbt3xsv4pKzcPQCw?p=preview
<body ng-controller="ExampleController">
<button ng-click="setButton()">set button</button>
<div content="snippetContentFirst"></div>
<div content="snippetContentSecond"></div>
<div content="snippetContentThird"></div>
<div>
<ul content="snippetContentFour"></ul>
</div>
</body>

Nested views inside angular ui bootstrap modal

Here is the entrypoint of my angularjs application. What I'm trying to create is an modal with multiple views.
(function() {
'use strict';
angular
.module('app', ['ui.router', 'ui.bootstrap'])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('modal', {
url: '/modal',
onEnter: ['$stateParams', '$state', '$modal', function ($stateParams, $state, $modal) {
$modal.open({
templateUrl: 'partials/modal.html',
backdrop: 'static'
});
}]
})
.state('modal.models', {
url: '/models',
templateUrl: 'partials/modal.models.html'
});
})
.run(function ($rootScope) {
$rootScope.$on("$stateChangeError", console.log.bind(console));
});
}());
and this is the main view
<!-- Modal -->
<div id="myModal">
<div class="modal-header">
<h2>Select your own car</h2>
</div>
<div class="modal-body">
<h4>Brands</h4>
<a ui-sref="modal.models">Models</a>
<div ui-view></div>
</div>
</div>
My problem is that when I click on the link ui-sref nothing happens. Why ui-router doesn't work inside a modal? I should pass in the second view that is the following named modal.models.html
<div>
<h1>Hello</h1>
</div>
For those like me that stumble along later, here's another path:
This plunker uses a simple CSS modal (rather than the fancy ui-bootstrap one) to accomplish the same end result --with the additional perk of 'sticky states' that persist underneath the modal interaction. Note that this supports substates within the modal in a uirouter-kosher way.
$stateProvider.state('modal', {
url: '/modal',
views: {
'modal': {
templateUrl: 'modal.html'
}
}
});
$stateProvider.state('modal.substate', {
url: '/substate',
template: '<h3>I\'m a substate in a modal'
});
(Credit to #christopherthielen for the example code.)
<script type="text/ng-template" id="modal.html">
<div class="modal-overlay fade">
<div class="modal-content">
<h2>Modal</h2>
This modal has a substate. <a ui-sref=".substate">Activate it</a>
<div ui-view></div>
<a ui-sref="app">Back to the app...</a>
</div>
</div>
</script>
In my own troubleshooting, the problem with the 'injected-onEnter' pattern detailed in the ui-router FAQ --which your code follows-- is that the ui-views within the injected template seem to be no longer visible/available to ui-router. Alas! Good thing there's not much required to simply roll-your-own modal window.

Categories