AngularJS – Update model based on URL #Angular #AngularJS

  • This is basically a follow up question this question: AngularJS – How to use $routeParams in generating the templateUrl?
  • See my code there for the proper context of this question.
  • Here are a few things I tried:

    I could probably create some method in my controller that would take these navs and update everything and navigate off and just add a to all the anchor tags.

  • However, one thing I really liked about AngularJS was being to use real URL-looking values for hrefs (e.g. ).
  • Is it not possible to update my model based on the changing URL when I route without going through some method on the controller?

$scope.$watch(function() {
return $location.path();
}, function(){

});

you should pass a function or a string that can be evaluated inside of the scope


@angular_recipe: AngularJS – Update model based on URL #Angular #AngularJS

This is basically a follow up question this question: AngularJS – How to use $routeParams in generating the templateUrl?. See my code there for the proper context of this question.

All the navigation part is working great, but now when I navigate, I want to update some properties in my

$scope

based on the

and

. I thought I could do something with

$watch

but I can’t seem to get anything to work. Here are a few things I tried:

$scope.$watch($location.path(), function() {…}); //result in js error $scope.$watch($location, function() {…}); //function runs once on page load, but not after that $scope.$watch($window.location, function() {…}); //function runs once on page load, but not after that $scope.$watch(window.location, function() {…}); //function runs once on page load, but not after that $scope.$watch(window.location.href, function() {…}); //results in js error

to all the anchor tags. However, one thing I really liked about AngularJS was being to use real URL-looking values for hrefs (e.g.

). Is it not possible to update my model based on the changing URL when I route without going through some method on the controller? I hope it makes sense what I’m asking.

$scope.$watch(function() { return $location.path(); }, function(){ … });

you should pass a function or a string that can be evaluated inside of the scope

There is currently no discussion for this recipe.

Angular.js Recipes are structured in a Cookbook format featuring recipes that contain problem statements and solutions. A detailed explanation follows each problem statement of the recipe. This is usually contained within the solution; however, an optional discussion section can often contain other useful information helping to demonstrate how the solution works.

Angular.js is a JavaScript-based open-source front-end web application framework mainly maintained by Google and by a community of individuals and corporations to address many of the challenges encountered in developing single-page applications. The JavaScript components complement Apache Cordova, the framework used for developing cross-platform mobile apps. It aims to simplify both the development and the testing of such applications by providing a framework for client-side model–view–controller (MVC) and model–view–viewmodel (MVVM) architectures, along with components commonly used in rich Internet applications.

Lost? Begin by working through Angular’s Getting Started Guide to get yourself up-and-running.

[SOLVED] AngularJS

You might also like More from author

Comments are closed, but trackbacks and pingbacks are open.