I am facing problem in call to controller in child ui state router.
URL is changing but controller not call.
no console error*
Please check code:
HTML
<a style="cursor: pointer;" class="btn btn-default btn-xs" ui-sref="interactions.details({id:n.id})">Detail</a>
Router
.state('interactions', {
url: '/interactions',
data: {
pageTitle: 'Interaction',
IsLoginPage: false
},
templateUrl: '../../modules/interaction/views/interaction.html',
controller: 'interactionCtl'
})
.state('interactions.details', {
url:'/details/:id',
data: {
pageTitle: 'Interaction Details',
IsLoginPage: false
},
templateUrl: '../../modules/interaction/views/interactionDetail.html',
controller:'interactionCtlDetails'
}).run(function ($rootScope, settings, $cookies, $http, $location, AuthenticationService, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
});
Controller
warApp.controller('interactionCtlDetails', ["$scope", '$rootScope','$stateParams', 'settings', 'categoryService', 'blockUI',
function ($scope, interactionService, $rootScope, $stateParams ,settings, categoryService, blockUI) {
var id = $stateParams.id;
console.log(id);
});
Annotation array should be in sync with the parameters in the function declaration.Here annotation array is not in sync with the parameters in the function declaration.
Second parameter in your annotation array is 'interactionService' but in function, thats 'rootScope'.
Try with below controller code
warApp.controller('interactionCtlDetails', ['$scope', '$rootScope', '$stateParams', 'settings', 'categoryService', 'blockUI',
function ($scope, $rootScope, $stateParams ,settings, categoryService, blockUI) {
var id = $stateParams.id;
console.log(id);
});
warApp.controller('interactionCtlDetails', ["$scope", '$rootScope','$stateParams', 'settings', 'categoryService', 'blockUI',
function ($scope, interactionService, $rootScope, $stateParams ,settings, categoryService, blockUI) {
var id = $stateParams.id;
console.log(id);
});
In above code you have function ($scope, interactionService, $rootScope, $stateParams ,settings, categoryService, blockUI) where you have interactionService which you have missed in your injection section
Simpler example of routing, maybe you can narrow down the problem by adding one controller at a time and build the rest. Fiddle or error will help.
angular.module('myApp').config(function ($stateProvider){
$stateProvider
.state('form', {
url:"/form",
views: {
"listColumn": {
templateUrl: "/form1.html",
controller:'myController1'
},
"formColumn": {
templateUrl: "/form2.html"
}
}
})
});
Definitely your controller should throw an error in console, the order of dependency parameters injected in the controller is wrong, you are missing interactionService
change it as,
warApp.controller('interactionCtlDetails', ["$scope", 'interactionService','$rootScope','$stateParams', 'settings', 'categoryService', 'blockUI',
function ($scope, interactionService, $rootScope, $stateParams ,settings, categoryService, blockUI) {
});
Related
Angular controller code:
(function ()
{
myApp.controller("LoginController", function ($scope, $http, $location, $window) {
$scope.Emp = {};
console.log("cont");
$scope.Submit = function (Emp) {
console.log("inside", $scope.Emp);
$http({
method: "POST",
url: '#/Test/Login',
data: $scope.Emp,
})
.then(function successCallback(response) {
console.log("response", response.data);
//$window.location.href = '/Home/Display';
if (response.data == "CORRECT UserName and Password") {
console.log("if condition")
$window.location.href = '/Test/Display';
}
else if (response.data == "INCORRECT UserName") {
alert("INCORRECT UserName or Password");
}
})
}
$scope.Register = function () {
$window.location.href = '/Test/Register';
}
});
})(angular.module('myApp'));
Angular Module Code:
var myApp = angular.module('myapp', ['ngRoute']);
app.config('$StateProvider', '$UrlRouteProvider','$scope', '$LogProvider', '$http', '$location', '$window',
function ($StateProvider, $UrlRouteProvider, $scope, $http, $location, $window)
{
$UrlRouteProvider.otherwise("/Login");
$StateProvider
.state('Login'),{
url:'/Login',
views:{
'#':{
templateUrl: "/Views/Home/Login.cshtml",
controller: 'LoginController'
}
}
}})
You only declared but didn't inject $LogProvider in config after $scope. Declaration and injection have to be same and in exact order as well.
It should be like :
app.config('$StateProvider', '$UrlRouteProvider', '$scope', '$LogProvider', '$http', '$location', '$window',
function ($StateProvider, $UrlRouteProvider, $scope, $LogProvider, $http, $location, $window)
{
$UrlRouteProvider.otherwise("/Login");
$StateProvider
.state('Login'),{
url:'/Login',
views:{
'#':{
templateUrl: "/Views/Home/Login.cshtml",
controller: 'LoginController'
}
}
}})
also there is # in your url: '#/Test/Login', check if you need it.
further, as per your code in question, it seems you don't require these much dependencies in config. In your code, I don't see any use of $scope, $LogProvider, $http, $location, $window..
I have AngularJS JavaScript code with a routing:
.when("/home/:id", {
templateUrl: " ",
controller: "TestController",
resolve: {
message: function(app, helpers, $route){
console.log($route);
return app.get({
user: 533,
id: 2
}).then(function (response) {
console.log($route);
return helpers.toArray(response.app);
});
}
}
});
My HTML link as:
Click
Also my TestController is:
.controller('TestController', ['$scope', '$http', '$routeParams', 'message', function ($scope, $http, $routeParams, message) {
$scope.appointments = message;
}])
When I try to get $route in resolve like as:
console.log($route);
I get nothing.
I am unable to get a $scope.$watch or $scope.$watchCollection to trigger when updating $state.current.data from a parent view.
I've created a plunker to demonstrate: http://plnkr.co/edit/d4hblq9QvnMOLQKxO9jc?p=preview
To use, navigate to the /main/1 path and click the 'change' button. You will see the $state.data get updated, yet the watchers never get a notification.
Here is the script.js source:
var app = angular
.module('MyApp', [
'ui.router'
])
.config(['$stateProvider', '$urlRouterProvider',
function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/main');
$stateProvider
// States
.state("main", {
data: {hello:'hello'},
controller:'mainController',
url:"/main",
templateUrl: "main_init.html"
})
.state("main.1", {
controller:'childController',
parent: 'main',
url:"/1",
templateUrl: 'form_1.html'
})
}])
.controller('mainController', function ($scope, $state, $timeout) {
$scope.Model = $scope.Model || {Name : "xxx"};
$scope.changeHello = function changeHello(){
console.log('about to change...');
$timeout(function(){
$state.current.data.hello = 'hi';
console.log('changed.');
},3500);
}
})
.controller('childController', function($scope, $state, $timeout){
$scope.changeStatus = 'nothing yet';
$scope.$watch($state.$current.data.hello, function(newObj){
if(newObj){
console.log('changed');
$scope.changeStatus = 'changed via watch';
}
});
$scope.$watchCollection($state.$current.data, function(newObj){
if(newObj){
console.log('changed');
$scope.changeStatus = 'changed via watchcollection';
}
});
$scope.$watchCollection($state.$current, function(newObj){
if(newObj){
console.log('changed');
$scope.changeStatus = 'changed via watchcollection $current';
}
});
$scope.$watchCollection($state.current, function(newObj){
if(newObj){
console.log('changed');
$scope.changeStatus = 'changed via watchcollection current';
}
});
})
app.run(
['$rootScope', '$state', '$stateParams',
function ($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
}
])
$watch is looking for an event that is the value of your variable, $state.current.data.hello. This is what it's doing:
$scope.$watch('hello', function(newObj)...
Put your variable in quotes and the $watch will work as expected:
$scope.$watch('$state.current.data.hello', function(newObj)...
I'm making the transition from NgRoute to ui-router using angular 1.0.8. Due to the fact I had some nested views I had to reload the routes which worked fine with ngRoute. I'm having the same issue with ui-router, is there an equivalent method I can use for this at all?
I'm using ui-router 0.2.10.
Edit:
I've created a fiddle to illustrate the issue that I'm having loading a basic view using ui-router.The view is requested and I can see it in the network inspector. The thing I can get my head around is why the view in not being injected into the template.
What am I missing here?
.run(function ($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
});
You are looking for the reload() function:
$state.reload()
http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state
You would put this in a controller like so:
var app angular.module('app', ['ui.router']);
app.controller('ctrl', function ($scope, $state) {
$scope.reload = function(){
$state.reload();
}
});
Edit 1
This is wrong.
.run(function ($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
});
You just need to inject $state into the controller you want to use it in.
angular.module('app').controller('Ctrl', ['$scope', '$state', function($scope,$state)
// Use $state here
$state.go('stateName');
{}]);
You forgot to inject ui.router, do that and it works fine.
https://jsfiddle.net/Christian_Schiffer/wLybswL9/1/
var myapp = angular.module('myapp', ['ui.bootstrap', 'ui.router']).config(function ($stateProvider, $urlRouterProvider) {
$stateProvider.state('one', {
url: '/',
templateUrl: '/one.html',
controller: 'MyCtrl'
})
$urlRouterProvider.otherwise('/');
}).run(function ($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
});
myapp.controller('MyCtrl', function ($scope) {
$scope.templateUrl = '/mobileTemplate.html';
});
myapp.controller('AccordionDemo', function ($scope) {
$scope.groups = [{
title: "Dynamic Group Header - 1",
routePath: "one.html",
number: 1,
current: true
}, {
title: "Dynamic Group Header - 2",
routePath: "two.html",
number: 2,
current: false
}];
});
I cannot get access to methods in my Angular Factory? I get "TypeError: Object # has no method 'method1'" error. My angular app looks like this...
myApp.js
var myApp = angular.module('myAngApp', [])
myApp.config(function ($routeProvider, $httpProvider) {
$routeProvider
.when('/list',
{
controller: 'ListController',
templateUrl: 'partials/list.html'
})
.when('/reports/:reportId',
{
controller: 'DetailController',
templateUrl: 'partials/report.html'
})
})
factory.js
myApp.factory('factory1', function(){
var factory = {};
factory.method1 = function() {
console.log('method1');
}
factory.method2 = function() {
console.log('method2');
}
return factory;
});
ListController.js
function ListController($scope, $location, $http, $route, $rootScope, factory1) {
factory1.method1();
}
ListController.$inject = ['$scope', '$location', '$http', '$route', '$rootScope', 'factory1'];
try this...
myApp.controller('ListController', [
'$scope',
'$location',
'$http',
'$route',
'$rootScope',
'factory1',
function ($scope, $location, $http, $route, $rootScope, factory1) {
factory1.method1();
}]);
instead of your current function ListController and the $inject statement
jsfiddle http://jsfiddle.net/NuCZz/