ncuillery / angular-breadcrumb

Generate a breadcrumb from ui-router's states
http://ncuillery.github.io/angular-breadcrumb/
MIT License
785 stars 183 forks source link

Controller as syntax? #102

Open marekpw opened 9 years ago

marekpw commented 9 years ago

Is it possible to either set the label from the controller (which in my opinion would be cleaner if the label is dynamic) or define a property to use on the controller itself?

As it is now, from what I can see, the only way to set dynamic label is to use the scope, which I'd like to avoid.

vjanssens commented 9 years ago

I think it would be handy to update the breadcrumb through a function in the controller. I've tried everything to pass a variable inside the label but could not get it to work. I originally tried it with the Controller as vm-syntax which did not work and afterwards without it and just $scope but that didn't work either.

EDIT: made a plunkr where the functionality actually works with ControllerAs syntax, awesome! http://plnkr.co/edit/xR5gcMLT9GK4ZFh1NqVU?p=preview I'm not sure how this differs with the project I'm working on, so i'd have to dig deeper to find the problem.

marekpw commented 9 years ago

Nice job with the controller as syntax, I'll have to try it out soon myself.

Though I'd still welcome an ability to change the breadcrumb from the controller itself using some sort of a service, perhaps also affect the whole tree somehow (there is a case where I need to create nested breadcrumbs for forums with unlimited depth of categories), but that's offtopic.

rucustodio commented 9 years ago

I also cannot get this to work when i pass in the var as "vm" to the breadcrumb label. Please help?

tuempeltaucher commented 8 years ago

I modified the code and its pretty easy to add a "setLastStepLabel" function.

        setLastStepLabel: function(label) {
            $state.$current.self.ncyBreadcrumbLabel = label;
        },

after the getLastStep function

in my controller: $breadcrumb.setLastStepLabel("lkjasdlkfjalksdjf");

I hope that there is a better solution in angular 2.0

tuempeltaucher commented 8 years ago

I found a "better" solution. The problem is that ncy-angular-breadcrumb does not get the correct scope, the scope-guessing-code is broken. I added a new function to set the desired scope.

after the getLastStep function: setLastViewScope: function(scope) { $lastViewScope = scope; }

Controller.js: var vm = this; $breadcrumb.setLastViewScope($scope); [...] vm.foo = "bar";

and in the state config: label: "{{ vm.foo }}",

devakone commented 8 years ago

A simpler way is also to $scope.$emit('$viewContentLoaded'); in your controller once you know for sure your page has loaded, in your activate() function for example. Outside of messing with the source code, the only time the $lastViewScope variable is updated currently is when that event is fired. Best case scenario moving forward though would be adding the $setLasViewScope function to the $breadcrumb API.