I have very little experience with javascript. I need to add a menu by clicking an item. We were asked to create it from scratch without using any library, such as bootstrap linkers or jQuery.
We use Angularjs. In angular, I want to know the correct method for creating new elements. Something like that we did not do document.createElement .
I am adding some of the code for you guys to better understand what I want to do.
Menu directive
.directive('menu', ["$location","menuData", function factory(location, menuData) { return { templateUrl: "partials/menu.html", controller: function ($scope, $location, $document) { $scope.init = function (menu) { console.log("init() called"); console.log("$document: " + $document); if (menu.selected) { $scope.tabSelected(menu); } } $scope.creteMenu = function(menuContent){ //This is to be called when the action is an array. } $scope.tabSelected = function(menu){ $location.url(menu.action); $scope.selected = menu; } $scope.click = function (menu) { if (typeof (menu.action) == 'string') { $scope.tabSelected(menu); } } }, link: function (scope, element, attrs) { scope.menuData = menuData; } }; }])
Menu data in the service.
.value('menuData', [{ label: 'Process-IDC', action: [] }, { label: 'Dash Board', action: '/dashboard', selected: true }, { label: 'All Jobs', action: '/alljobs', selected: false }, { label: 'My Jobs', action: '/myjobs', selected: false }, { label: 'Admin', action: '/admin', selected: false }, { label: 'Reports', action: '/reports', selected: false }]);
If you notice that the action of the Process-IDC menu is an array, it will contain more menus with actions in it, and it should be open in the submenu.
Menu.html (partial)
<ul class="menu"> <li ng-class="{activeMenu: menu==selected}" ng-init="init(menu)" data-ng-click="click(menu)" data-ng-repeat="menu in menuData">{{menu.label}}</li> </ul>