I use ui-router jumping from the navigation or jumping from the svg page just using same contoller ,same html model, same css file. Mainly through the parameter to determine the data switch. Jumping from navigation is normal . Jumping from svg page that the contoller will execute twice .the first execute is normal , however the second will exexute first state(). Finally, the parameter which is sent by sender will change to first value? sorry,I'm not good at english. please look at the follow code. Thank you very much for those who can help me!
this is state()
// .state('main.visual_2d_equipdata2', {
// url: '/visual/2D/equipdata',
// templateUrl: '../views/visual/2D/equipdata.html',
// controller: 'Visual2dEquipDataController',
// css: '../static/visual/2D/css/equipdata.css',
// params: { 'id':2 }
// })
// .state('main.visual_2d_equipdata3', {
// url: '/visual/2D/equipdata',
// templateUrl: '../views/visual/2D/equipdata.html',
// controller: 'Visual2dEquipDataController',
// css: '../static/visual/2D/css/equipdata.css',
// params: { 'id': 3 }
// })
// .state('main.visual_2d_equipdata4', {
// url: '/visual/2D/equipdata',
// templateUrl: '../views/visual/2D/equipdata.html',
// controller: 'Visual2dEquipDataController',
// css: '../static/visual/2D/css/equipdata.css',
// params: { 'id': 4 }
// })
// .state('main.visual_2d_equipdata5', {
// url: '/visual/2D/equipdata',
// templateUrl: '../views/visual/2D/equipdata.html',
// controller: 'Visual2dEquipDataController',
// css: '../static/visual/2D/css/equipdata.css',
// params: { 'id': 5 }
// })
// .state('main.visual_2d_equipdata6', {
// url: '/visual/2D/equipdata',
// templateUrl: '../views/visual/2D/equipdata.html',
// controller: 'Visual2dEquipDataController',
// css: '../static/visual/2D/css/equipdata.css',
// params: { 'id': 6}
// })
this is my navigation jumper function
$scope.jumper = function(sref) {
$scope.nav_total = false;
if (sref == "about") {
$scope.about_window = true;
} else if (sref == "event") {
window.open("#/" + sref, target = "_blank");
} else if (sref == "info") {
window.open("#/" + sref, target = "_blank");
} else if (sref == "config") {
$state.go("main.config.personnel");
} else {
sref = sref.replace(/\./g, '_');
console.log(6666);
$state.go("main." + sref);
}
};
this is svg contoller
// switch(parseInt(id)){
// case 1:
// $state.go("main.visual_2d_equipdata1" );
// break;
// case 2:
// $state.go("main.visual_2d_equipdata2");
// break;
// case 3:
// $state.go("main.visual_2d_equipdata3");
// break;
// case 4:
// $state.go("main.visual_2d_equipdata4");
// break;
// case 5:
// $state.go("main.visual_2d_equipdata5");
// break;
// case 6:
// $state.go("main.visual_2d_equipdata6");
// break;
// }
this is main contoller which will execute twice when jumping from svg contoller
angular.module('app.con.visual.2d.equipdata', []).controller('Visual2dEquipDataController', ['$scope', '$state', 'EventService', 'Visual2dService', '$stateParams', function($scope, $state, EventService, Visual2dService, $stateParams) {
$scope.equipNum = $stateParams.id;
console.log( $stateParams);
console.log(999);
var getIDNumber = function(){
switch($stateParams.id){
case 2:
var equipId = "1.0005";
break;
case 1:
var equipId = "1.0004";
break;
case 3:
var equipId = "1.0004";
break;
case 4:
var equipId = "1.0004";
break;
case 5:
var equipId = "1.0005";
break;
case 6:
var equipId = "1.0004";
break;
}
}
// console.log( equipId);
var aircondition = function() {
$scope.actual_ac = { "access_token": sessionStorage.access_token, "type": "actual_object", "id": [ $scope.equipId ] };
Visual2dService.data_2d($scope.actual_ac).then(function(res) {
if (res.errcode == "00000") {
console.log(res);
var analog = res.data[0].analog;
$scope.analogs = new Array();
for (var i = 0; i < analog.level.length; i++) {
var item = new Object();
item["level"] = analog.level[i];
item["property"] = analog.property[i];
// item["unit"] = analog.unit[i];
item["value"] = analog.value[i];
$scope.analogs.push(item);
};
var digit = res.data[0].digit;
$scope.digits = new Array();
for (var i = 0; i < digit.level.length; i++) {
var item = new Object();
item["level"] = digit.level[i];
item["property"] = digit.property[i];
if (digit.level[i] == 0) {
item["show"] = "check";
} else {
item["show"] = "exclamation";
};
$scope.digits.push(item);
};
} else {
console.info(res.errmsg);
};
});
};
var init = function() {
getIDNumber();
aircondition();
// airscreen();
// setTimeout(init, sessionStorage.reload_timeout);
};
init();
}]);
Note that
console.log( $stateParams);
console.log(999);
when click 2 jump from svg page will log {id: 2} 999 {id: 1} 999
ui-router
partly works by resolving a given url to a particular state (for example when you reload the page), so it is not recommended to have same url for different states.
For your case if you don't mind having the id
inside the url, the state definition can be greatly simplified to
.state('main.visual_2d_equipdata', {
url: '/visual/2D/equipdata/:id',
templateUrl: '../views/visual/2D/equipdata.html',
controller: 'Visual2dEquipDataController',
css: '../static/visual/2D/css/equipdata.css'
})
So at the destination you can use $stateParams.id
as well.
Some changes need to be done when navigating to this state
$state.go('main.visual_2d_equipdata', {id:2});
or
ui-sref="main.visual_2d_equipdata({id:2})"