angularjs - How to pass `controllers` status to `directive` template function? -
accoding current page, need change template. question is, how pass current
page controller
directives
template method?
here try:
var myapp = angular.module('myapp', []); myapp.controller('main', function ($scope) { $scope.template = "homepage"; }); var gettemplate = function (page) { //i need $scope.template params if (page == "homepage") { return "<button>one button</button>" } if (page == "servicepage") { return "<button>one button</button><button>two button</button>" } if (page == "homepage") { return "<button>one button</button><button>two button</button><button>three button</button>" } } myapp.directive('gallerymenu', function () { return { template : gettemplate(template), //$scope.template need pass link : function (scope, element, attrs) { console.log(scope.template); } } })
update
i trying this, still getting error. correct way inject $route
directive
?
var gallerymenu = function ($route, $location) { return { template : function () { console.log($route.current.classname); //i not getting! }, link : function () { } } } angular .module("tcpapp", ['$route', '$location']) .directive('gallerymenu', gallerymenu);
you can call $routeparams
on directive declaration, use inside template
function.
myapp.directive('gallerymenu', ['$routeparams', function($routeparams) { return { template: function () { var page = $routeparams.page || 'homepage', // define fallback, if $routeparams doesn't have 'page' param output; switch (page) { case "servicepage": output = "<button>one button</button><button>two button</button>"; break; default: case "homepage": output = "<button>one button</button>"; /* note: or other, confusing tell 1 use output = "<button>one button</button><button>two button</button><button>three button</button>"; */ break; } return output; }, link: function(scope, element, attrs) { /* ... */ } } }]);
edit 1:
if using ui-router
switch $routeparams
$stateparams
.
Comments
Post a Comment