I am trying to display select box using directive. But my default option is not display.
I do like this
<div>
<select ng-init="userselected = vm.data[0]"
ng-model="userselected"
ng-options="option.name for option in vm.data">
</select>
</div>
Here is my code http://plnkr.co/edit/Q1e8u0okOa4looLcb2YO?p=preview
.controller('f',function($http){
var l=this;
$http.get('data.json').success(function(data){
l.data=data;
})
})
Can we load data before calling controller and loading the html file using resolve?
Can we load data before calling controller and loading the html file using resolve?
Yes. The way to do that would be to resolve the dataset you want to expose onto your view before initialising the controller attached to said portion of the view.
I can think of three ways off of the top of my head;
// state definition
.state('myState', function () {
resolve: {
dataset: function (Service) {
return Service.getData();
}
},
controller: 'Controller'
})
// controller definition
.controller('Controller', function (dataset) {
this.dataset = dataset;
})
The resolved data will be available under the given name in the controller attached to the state definition (through the definition itself, or with a ng-controller
at the root of your state template).
// route definition
.when('/path', {
resolve: {
dataset: function (Service) {
return Service.getData();
}
},
controller: 'Controller'
})
// controller definition
.controller('Controller', function (dataset) {
this.dataset = dataset;
})
Pretty much the exact same way as the ui-router example.
Defer the rendering of your template, by waiting for the promise to resolve.
.controller('outer', function ($timeout) {
this.resolved = false;
this.dataset = [];
$timeout(function () {
this.resolved = true;
this.dataset = [ {}, {} ];
}.bind(this), 1500);
})
.controller('inner', function () {
console.log('I just ran!');
})
<div ng-controller="outer as o">
<div ng-controller="inner as i" ng-if="o.resolved">
{{o.dataset}}
</div>
</div>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments