AngularJS Router and ui-view - angularjs

Hi I have having trouble getting my router to work. I have created a page called lookup.html and I've added it to my router file, but it's not loading in the ui-view when I go to the url I've specified in the router. I've created a plnkr and hope it will help.
http://plnkr.co/edit/II7Mv7jNYENhJHKX1ht5?p=linter
here's my router:
(function () {
'use strict';
angular
.module('crm.ma')
.config(config);
config.$inject = ['$stateProvider', '$urlRouterProvider'];
function config($stateProvider, $urlRouterProvider) {
$stateProvider
.state('login', {
url: '/login?token',
data: {
skipAuth: true
},
resolve: {
valid: function (authService, $stateParams, sessionService, $state) {
if (!sessionService.getSession()) {
if ($stateParams.token) {
sessionService.setSession($stateParams.token);
} else {
sessionService.removeSession();
}
}
authService.login();
}
}
})
.state('main', {
abstract: true,
templateUrl: 'app/components/common/layout.html',
controller: 'mainController as vm',
data: {
pageTitle: 'Homepage'
}
})
.state('main.index', {
url: '/',
templateUrl: 'app/main/main.html',
controller: 'mainController as vm',
data: {
pageTitle: 'Homepage'
}
})
.state('search', {
url: '/search',
templateUrl: 'app/advancedsearch/advanced-search.html',
controller: 'advancedsearch.controller as vm',
data: {
pageTitle: 'Search'
}
})
.state('lookup', {
url: '/lookup',
templateUrl: 'app/components/common/lookup.html',
controller: 'LookUpCtrl as vm',
data: {
pageTitle: 'Look Up'
}
})
.state('dealer', {
url: '/dealer',
templateUrl: 'app/components/common/layout.html',
controller: 'dealerController as vm',
data: {
pageTitle: 'Dealer'
}
});;
$urlRouterProvider.otherwise('/');
}
})();
and here is my lookup.html page.
<div>
<div>Lookup Results</div>
<table>
<thead>
<tr>
<td>Acc. ID</td>
<td>Acc. Name</td>
<td>Acc Address</td>
<td>City</td>
<td>Zip</td>
<td>Phone</td>
<td>Parent Name</td>
<td>Account Type</td>
<td>Account Status</td>
<td>Credit Term</td>
</tr>
</thead>
<tbody>
<tr ng-repeat="result in vm.results">
<td>{{ result.accountId }}</td>
<td>{{ result.accountName }}</td>
<td>{{ result.address }}</td>
<td>{{ result.city }}</td>
<td>{{ result.state }}</td>
<td>{{ reuslt.zip }}</td>
<td>{{ result.phone }}</td>
<td>{{ result.parentName }}</td>
<td>{{ result.accountType }}</td>
<td>{{ result.accountStatus }}</td>
<td>{{ result.accountStatus }}</td>
<td>{{ result.creditTerm }}</td>
</tr>
</tbody>
</table>
If any further info is needed please let me know. I didn't write this, and I'm still a little unsure on how to use the router.

Maybe you has imported angular-route.js and not angular-ui-route.js; they are differents. With angular-route.js you should use:
<div ng-view></div>
And in angular-ui-route.js you should use:
<div ui-view></div>
Another problem maybe is the first line of you router file, it should be like:
'use strict';
angular.module(.........

Related

Angular route causing an infinite loop

I have a an angular single page app that loads correctly on for the home and about pages. But the edit route goes into an infinite loop of errors.
The error is
SyntaxError: Unexpected token '<'
I call editCaldera() using a button in a table using ng-click
<table class="table">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">Borrower1</th>
<th scope="col">Email</th>
<th scope="col">Address</th>
<th scope="col">ContactNumber</th>
<th scope="col">Lender</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="caldera in calderas">
<td>{{ caldera._id }}</td>
<td>{{ caldera.Borrower1 }}</td>
<td>{{ caldera.Email }}</td>
<td>{{ caldera.Address }}</td>
<td>{{ caldera.ContactNumber }}</td>
<td>{{ caldera.Lender }}</td>
<td><button ng-click="editCaldera(caldera._id)">Edit</button></td>
<td><button ng-click="deleteCaldera(caldera._id)">Delete</button></td>
</tr>
</tbody>
</table>
Below is my main javascript file.
var calderaApp = angular.module('calderaApp', ['ngRoute']);
calderaApp.config(function($locationProvider, $routeProvider) {
$locationProvider.html5Mode(true);
$routeProvider
// route for the home page
.when('/', {
templateUrl: 'pages/home.html',
controller: 'mainController'
})
// route for the about page
.when('/about', {
templateUrl: 'pages/about.html',
controller: 'aboutController'
})
.when('/edit/:id', {
templateUrl: 'pages/edit.html',
controller: 'editController'
})
});
calderaApp.controller('mainController', function($scope, $http, $location) {
// when landing on the page, get all todos and show them
$http.get('/findall')
.success(function(data) {
console.log(data);
$scope.calderas = data;
})
.error(function(data) {
console.log('Error: ' + data);
});
// when submitting the add form, send the text to the node API
$scope.editCaldera = function(id) {
console.log('editCaldera function')
var earl = '/edit/' + id;
$location.url(earl);
};
});
calderaApp.controller('aboutController', function($scope, $http) {
$scope.message = 'Look! I am an about page.';
});
calderaApp.controller('editController', function($scope, $http, $routeParams) {
console.log('edit')
});
I need the id field to then make an API call but even before making this call and just with a very basic controller a loop occurs.

angularjs > router resolve > "Error: [$injector:unpr] Unknown provider"

im trying to add a resolve\promise to my project, so when you ask for a page it will load up only after receiving the json from the server.
this is my js code:
'use strict';
angular.module('myApp.show', ['ngRoute'])
.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/show', {
templateUrl: 'views/show.html',
controller: 'showCtrl',
resolve: {
booksList: function ($http) {
return ($http.get('data/books.json')
.then(function (data) {
return data;
}));
}
}
});
}])
.controller('showCtrl', ['booksList', '$scope', function (booksList, $scope) {
$scope.books = booksList;
$scope.removeRow = function (productIndex) {
if (window.confirm("are you sure?")) {
$scope.books.splice(productIndex, 1);
}
}
}])
but this is what i get:
Error: [$injector:unpr] Unknown provider: booksListProvider <- booksList <- showCtrl
i kinda new to angular, but i followed several tutorials and while it worked in the video - i keep getting errors.
html:
<div class="table-responsive">
<div ng-app="myApp.show" ng-controller="showCtrl"> <!-- ctrl -->
<table st-table="rowCollection" class="table table-striped">
<thead>
<tr>
<th st-sort="author">Author</th>
<th st-sort="date">Date</th>
<th st-sort="title">Title</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="(bookIndex, book) in books">
<td class="col-md-3">{{ book.author }}</td>
<td class="col-md-3">{{ book.date | date }}</td>
<td class="col-md-4">{{ book.title | beutifyTitle }}</td>
<td class="col-md-1"><ng-include src="'views/partials/editBook.html'"></ng-include></td>
<td class="col-md-1"><button class="btn btn-warning" ng-click="removeRow()">Delete</button></td>
</tr>
</tbody>
</table>
</div>
</div
You should be removing ng-controller="showCtrl" from your template. The reason being is, you are assing showCtrl via router already. But as you are again wrote ng-controller over inline template in that case it fails to get booksList resolve provider.

Pass value to another controller in angularjs

I am new to AngularJS.
I have a div assigned controller to controller1. In this div, I am showing EmployeeList with three links for View, Edit,Delete for each employee.
I have another div assigned controller to controller2. By clicking the view link on any employee, I want to show full details in second div. Both divs are in same page.
How do I do that? Below is what I tried.
<div ng-controller="employeeController" style="border:solid black 3px;float:left;padding:5px">
<h3 style="color:#ff6a00">List Of Employees</h3>
<table>
<tr>
<th hidden>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Full Name</th>
<th>Actions</th>
</tr>
<tr ng-repeat="employee in employees">
<td hidden><strong>{{ employee.ID }}</strong></td>
<td>{{ employee.FName }}</td>
<td>{{ employee.LName }}</td>
<td><strong>{{ employee.FName + ' ' + employee.LName }}</strong></td>
<td>
<a data-ng-click="GetEmployeeByID({{employee.ID}})" href="javascript:;">View</a>
</td>
</tr>
</table>
</div>
<div ng-controller="employeeByIDController" style="border:solid black 3px;float:right;padding:5px">
<h3 style="color:#ff6a00">Employee Details</h3>
<table>
<tr>
<th hidden>ID</th>
<th>Full Name</th>
<th>Qualification</th>
<th>Gender</th>
<th>Phone</th>
<th>Email</th>
<th>Address</th>
<th>City</th>
</tr>
<tr>
<td hidden><strong>{{ employeeByID.ID }}</strong></td>
<td>{{ employeeByID.FName + ' ' + employeeByID.LName }}</td>
<td>{{ employeeByID.Qualification }}</td>
<td>{{ employeeByID.Gender }}</td>
<td>{{ employeeByID.Phone }}</td>
<td>{{ employeeByID.Email }}</td>
<td>{{ employeeByID.Address }}</td>
<td>{{ employeeByID.City }}</td>
</tr>
</table>
</div>
The code in factory is as below.
mainApp.factory('EmployeeFactory', function ($http) {
return {
GetEmployeeList: function () {
return $http({
method: 'GET',
url: '/AngularEmployee/EmployeeList'
});
}
}
return {
GetEmployeeByID: function ($id) {
return $http({
method: 'GET',
url: '/AngularEmployee/EmployeeByID',
params: { empID : id }
});
}
}
});
I have added controller as follows.
mainApp.controller("employeeController", ['$scope', 'EmployeeFactory', function ($scope, EmployeeFactory) {
$scope.GetEmployeeList = function () {
EmployeeFactory.GetEmployeeList().success(function (data) {
$scope.employees = data;
}).error(function (data) {
$scope.error = "An Error has occured while Loading users! " + data.ExceptionMessage;
});
};$scope.GetEmployeeList();
}]);
Angular has a pubsub mechanism for this. It's well documented. E.g.:
https://toddmotto.com/all-about-angulars-emit-broadcast-on-publish-subscribing/
if you want to share data between two controllers in angularJs , using a service or factory is a better option as service/factory in angularJs are injectable.But service/factory is a singleton object so be aware of the fact that same object of the service/factory will be shared by all the controllers.
Taking your example ,
you can create another service :
mainApp.service('sharedService',function(){
this.employee={};
this.getter=function(){
return this.employee;
};
this.setter=function(emp){
this.employee=emp;
};
});
Then you can share this service across your controllers to get or set employee.
mainApp.controller("employeeController", ['$scope', 'EmployeeFactory', 'sharedService', function($scope, EmployeeFactory, sharedService) {
$scope.GetEmployeeList = function() {
EmployeeFactory.GetEmployeeList().success(function(data) {
$scope.employees = data;
sharedService.setter($scope.employees);//set in shared service
}).error(function(data) {
$scope.error = "An Error has occured while Loading users! " + data.ExceptionMessage;
});
};
$scope.GetEmployeeList();
}]);
And then in employeeByIDControlle ,
mainApp.controller("employeeByIDController", ['$scope', 'EmployeeFactory', 'sharedService', function($scope, EmployeeFactory, sharedService) {
$scope.employees = sharedService.getter();//get from shared service
}]);

react in large angular app

So I have an angular large web application, I have a few pages where I would want to use React to decrease the rendering time of the page (>3000 rows).
While searching for a solution I encountered this react plugin: http://bebraw.github.io/reactabular/
It lets me add a datatable with editing options and more - perfect for my needs.
The problem is that there is no easy way to include it in a prebuilt angular app.
Since I am new to react, I wanted to integrate it to my current angular application and I am searching the web for answers for a long time.
I am looking for a way to integrate it to my current code, that looks like the following:
var contactManager = angular.module('contactManager', ['ngMaterial','ngCookies','ngRoute','loadingOnAJAX','truncate','chart.js','xeditable','datatables','ui.calendar',
'xeditable', 'ngFileUpload','ui.bootstrap','html5.placeholder','luegg.directives','angular-svg-round-progress','ngProgress','ngDraggable'])
.run(function($rootScope, ngProgress, editableOptions) {
editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
$rootScope.$on('$routeChangeStart', function(ev,data) {
ngProgress.start();
});
$rootScope.$on('$routeChangeSuccess', function(ev,data) {
// Close menu on mobiles
ngProgress.complete();
});
})
.config(['$routeProvider','$locationProvider','$controllerProvider', function($routeProvider, $locationProvider, $controllerProvider) {
$routeProvider
.when('/index', {
templateUrl: 'partials/adminPanel.html',
controller: 'GroupPanelCtrl',
resolve: GroupPanelCtrl.resolve
})
.when('/profile/:id', {
templateUrl: 'partials/profile.html',
controller: 'ProfileCtrl'
})
.when('/group/:id/contacts', {
templateUrl: 'partials/group-contacts.html',
controller: 'GroupContactsInfo',
resolve: GroupContactsInfo.resolve
})
and the HTML:
<table datatable="ng" class="table table-bordered table-hover dataTable" role="grid" data-page-length="100">
<thead>
<tr>
<th></th>
<th>{{texts.fullName}}</th>
<th>{{texts.title}}</th>
<th>{{texts.mobile}}</th>
<th>{{texts.email}}</th>
<th>{{texts.department}}</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="contact in contacts track by $index">
<td ng-click="clickContact()">{{img}}
</td>
<td ng-click="clickContact()">
<a>
{{::contact.fullName}}
</a>
</td>
<td ng-click="clickContact()">
{{::contact.title|| ""}}
</td>
<td ng-click="clickContact()">
{{::contact.phoneFormatted}}
</td>
<td ng-click="clickContact()">
<span>
{{::contact.email}}
</span>
</td>
<td ng-click="clickContact()">
{{::contact.department|| ""}}
</td>
<td ng-click="deleteContact(contact, $index)">
<span class="fa fa-trash showOnhover"></span>
</td>
</tr>
</tbody></table>
If you need to call Angular code from a non-Angular app, try https://github.com/bcherny/ngimport. It lets you require/import Angular services from non-Angular files.

ngTable doesn't work with UI-Router

I always used the directive ngTable with ngRoute module. However, I recently migrated to the UI-Router for better fit the project I'm developing right now.
But for some reason, ngTable does not work correctly with the UI-Router.
The table is generated normally, however the settings do not.
It is not created neither the header nor paging. As the group also is not working.
I also tried to create the table header manually, but I noticed that it disappears when I add the attribute "ng-include" with the value "templates.header".
I'm used the following versions:
ngTable - 1.0.0-beta.9
UI-Router - 0.2.15
Someone have come across this problem? I can not find the solution anywhere.
JavaScript:
app.controller("users", function($http, $rootScope, $stateParams, $scope, NgTableParams) {
$scope.users = {};
$http({
method: "POST",
url: "./controllers/users/read.php",
data: {
id: $stateParams.id,
language_id: $rootScope.globals.language.id
}
}).success(function(response) {
$scope.tableParams = new NgTableParams({
count: 10,
sorting: {
name: "asc"
}
}, {
dataset: response
});
$scope.users = response;
});
});
HTML:
<table ng-table="tableParams" class="table table-bordered table-striped">
<tr ng-repeat="row in $data">
<td data-title="'txt.pages.users.fields.name' | translate" sortable="'name'" width="45%">
{{ row.name }}
</td>
<td data-title="'txt.pages.users.fields.email' | translate" sortable="'email'" width="45%">
{{ row.email }}
</td>
<td data-title="'txt.pages.users.fields.actions' | translate" width="10%">
<button ng-click="edit(row.id)" class="btn btn-warning btn-sm">
<icon class="glyphicon glyphicon-edit"></icon>
</button>
<button ng-click="delete(row)" class="btn btn-danger btn-sm">
<icon class="glyphicon glyphicon-remove"></icon>
</button>
</td>
</tr>
</table>
stateProvider:
app.config(function($stateProvider, $urlRouterProvider) {
$stateProvider.state("site", {
url: "/",
views: {
"outside#": {
templateUrl: "./template/site/index/read.html"
},
"inside#site": {
templateUrl: "./template/site/home/read.html"
}
}
}).state("cms", {
url: "/cms",
views: {
"outside#": {
templateUrl: "./template/cms/index/read.html"
}
}
}).state("dashboard", {
parent: "cms",
url: "/dashboard",
views: {
"inside#cms": {
templateUrl: "./template/cms/dashboard/read.html"
}
}
}).state("login", {
parent: "cms",
url: "/login",
views: {
"inside#cms": {
templateUrl: "./template/cms/login/read.html"
}
}
}).state("users", {
parent: "cms",
url: "/users",
views: {
"inside#cms": {
controller: "users",
templateUrl: "./template/cms/users/read.html"
}
}
});
});
I think you should use $users instead of $data in below line.
<tr ng-repeat="row in $data">

Resources