我有一个 View ,其中包含一个菜单和一个指令。我希望在页面加载时选择菜单中的第一项,然后填充指令的内容。
由于菜单的内容是从服务器文件加载的并且经常更改,我认为我不能使用默认路由,因为每次文件更改时路由都会更改名称。
events.html
<ul>
<li ng-repeat="event in events"><a href="#/{{event.title}}">{{event.title}}</a></li>
</ul>
以下列表项在页面加载时动态加载,但为了简洁起见,我将其缩短以内联加载事件。
controller.js
myApp.controller('controller', ['$scope',function($scope){
$scope.events = {title: 'event 1', title: 'event 2'};
}]);
我希望在页面加载时默认选择一个菜单项,例如 $scope.events[0]。
最佳答案
'$scope.events' 中有重复的键。我认为它应该是对象数组。
这可能有效:
$scope.$watch('events', function(newVal, oldVal){
if(newVal.length){
$scope.selected=$scope.events[0];
}
});
关于javascript - 页面加载时的默认菜单选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30792324/