Answer the question
In order to leave comments, you need to log in
Simple data loading in state from json?
There is such a simple mechanism:
<aside>
<ul ng-repeat="data in datas">
<li ui-sref="detail({id:data.id})">{{data.name}}</li>
</ul>
</aside>
<div class="main-container">
<div class="chenge-container">
<div ui-view></div>
</div>
</div>
myApp.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/detail');
$stateProvider
.state('detail', {
url: '/detail/:id',
templateUrl: 'pages/detail.html',
controller: 'DetailCtrl',
})
});
myApp.controller('MainCtrl', function($scope, $http){
$http.get("params.json").success(function(datas){
$scope.datas = datas;
})
});
myApp.controller('DetailCtrl', function($scope, $http, $stateParams){
$http.get($stateParams.id".json").success(function(data){
$scope.data = data;
})
});
[{
"id":1,
"name":"Moscow",
"detail":"That's an awesome message"
},{
"id":2,
"name":"Amsterdam",
"detail":"That's the best message ever"
},{
"id":3,
"name":"Tokio",
"detail":"I'M THE ONLY ONE"
}]
Answer the question
In order to leave comments, you need to log in
It seems to me, except how to pull out the entire file and then select the right one will not work.
1 option:
myApp.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/detail');
$stateProvider
.state('root', {
url: '',
resolve: {
promiseDatas: function ($state, $http) {
return $http.get("params.json");
}
},
controller: function ($state, promiseDatas) {
var vm = this;
vm.datas = promiseDatas.data;
$state.current.data.datas = promiseDatas.data;
},
controllerAs: 'root'
data: {
datas: {}
}
})
.state('detail', {
parent: 'root',
url: '/detail/:id',
templateUrl: 'pages/detail.html',
resolve: {
promiseData: function ($state, $stateParams) {
return _.findWhere($state.current.data.datas, { id: $stateParams.id })
}
},
controller: function (promiseData) {
var vm = this;
vm.data = promiseData;
},
controllerAs: 'detail'
})
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question