Daniel Platz
unread,Jul 12, 2013, 6:47:56 AM7/12/13Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ang...@googlegroups.com
Hi tere,
I am trying to build a service + directive to allow for something like a ng-view facility that is not routing-based but configured via explicit calls to a service.
I.e. something like a "show" call with paramters 1.) Name of a template to use, 2.) name of a controller to use and 3.) a set of properties to initialize/customize the controller with.
So, basically to a call
viewStack.pushView("ViewCtrl", "viewTemplate.html", { /* some props to initialize the ViewCtrl */});
that then injects the template with that particular controller and calls a init-function on that controller where the third argument is give as a parameter to this init-method.
Additionally, I want it later to be like logical stack of views where I can push and pop view from and to. Visually, this might then be augment by some fadeIn/out animations.
I haven't really implemented that part yet, but thats the final goal, in case you are wondering why i use the naming "Push" and "pop". Not sure yet, but i guess i will have to keep track of the scopeson the stack and bind/unbind them based on the current view/template shown.
Now to my actual question: As I am quiet new to angular and it was really hard for me to get this working (in my head), i.e. creating the controler manually and binding it to a scope; and also using a service as the interface that communications to the directive (see my code below): Is this good design? should I have done differently?
Still struggeling a little to get this view-stack implemented with the concepts of angular...
Any comments/hints are very welcome.
Thanks,
Daniel
app.controller('TestCtrl', function($scope, viewStack) {
$scope.push = function() {
viewStack.pushView("ViewCtrl", "viewTemplate.html", { /* some props to initialize the ViewCtrl */});
}
});
app.controller('ViewCtrl', function($scope) {
this.init = function(props) {
// set up scope based on props here
}
});
app.factory("viewStack", function($templateCache, $document, $compile, $rootScope) {
var stack = [];
return {
pushView: function(viewCtrl, template, props) {
$rootScope.$emit('showView', { 'controller': viewCtrl, 'template': template, 'props': props});
},
popView: function() {
// todo
}
};
});
app.directive('viewContainer', function factory($rootScope, $compile, $controller, $http, $templateCache) {
return {
restrict: 'A',
template: '<div style="position: absolute; width: 200px; height: 200px; z-index: 1; background-color: red;"></div>',
link: function($scope, $element, $attrs) {
console.log("link");
$rootScope.$on('showView',function(event, props) {
$http.get(props.template, {cache: $templateCache}).success(function(response) {
var ctrl = $controller(props.controller, { '$scope': $scope });
ctrl.init(props.props);
$element.html(response);
$compile($element.contents())($scope);
}).error(function() {
console.log("error");
});
});
}
};
});
<div view-container></div>
<button ng-controller="TestCtrl" ng-click="push()">