ng-init
前link
函數調用,所以嘗試此解決方案:
.directive('gettheme',function(){
return {
restrict:"AE",
template:"<select ng-init='themedata=theme[0]' ng-model='themedata' ng-options='item for item in theme' class='form-control'></select>",
link:{
pre:function(scope, element, attrs){
scope.theme=['macarons', 'infographic'];
}
}
}})
或者這一個:
.directive('gettheme',function(){
return {
controller:function($scope){
$scope.theme=['macarons', 'infographic'];
},
restrict:"AE",
template:"<select ng-init='themedata=theme[0]' ng-model='themedata' ng-options='item for item in theme' class='form-control'></select>",
}
})
或本:
.directive('gettheme',function(){
return {
restrict:"AE",
template:"<select ng-model='themedata' ng-options='item for item in theme' class='form-control'></select>",
link:function(scope, element, attrs){
scope.theme=['macarons', 'infographic'];
scope.themedata=scope.theme[0];
}
}})