当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > angular中使用路由和$location切换视图

angular中使用路由和$location切换视图

2018年06月18日  | 移动技术网IT编程  | 我要评论

我们可以利用angular的$route服务来定义这样一种东西:对于浏览器所指向的特定url,angular会加载并显示一个模板,并实例化一个控制器为模板提供内容。

在应用中,你可以通过调用$routeprovider服务上的函数来创建路由,把需要创建的路由当成一个配置块传给这些函数即可。 伪代码如下:

复制代码 代码如下:

var somemodule = angular.module('somemodule',[...module dependencies...])
somemodule.config(function($routeprovider){
    $routeprovider.
      when('url',{controller:acontroller,templateurl:'/path/to/template'}).
      when(...other...).
      otherwise(...what to do ...);
});

方法很简单,代码也很简洁,但是却非常实用,希望小伙伴们能够喜欢。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网