传递变量到$ routeProvider在视图之间的角度? - angularjs [英] pass a variable into $routeProvider in angular between views? - angularjs

查看:99
本文介绍了传递变量到$ routeProvider在视图之间的角度? - angularjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道,如果有一个特定角度的方式,试图实现这一目标。
我有一些看法的页面。当用户点击一个锚的看法发生改变,很简单。我很好奇,如果当用户点击,是有可能存储变量(比如跨度),然后将它传递给 $ routeProvider 加载内容多一点动态?

Wondering if there's an 'angular specific' way to try and achieve this. I have a page with some views. When a user clicks an anchor, the views change, simple enough. What I'm curious is, if when the user clicks, is it possible to store a variable (say the span) then pass it to the $routeProvider to load the content a bit more dynamically?

所以锚标签#名称1,显示该视图的时候,我可以通过名称1到 nameHolder 作为一个变量来加载具有相同名称的一些文件。

so for anchor tag #name1, when that view is displayed, I can pass "name1" into nameHolder as a variable to load some files with same name.

HTML

<a href="#name1"><span>name1</span></a>

JS

var nameHolder = [];
var sampleApp = angular.module('sampleApp', ['ngRoute']);

sampleApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/' + nameHolder, {
        templateUrl: 'templates/individkcd.html',
        controller: 'individ',
        nameofuser: nameHolder
    }).
      when('/', {
        templateUrl: 'templates/home.html'
    });
}]);


sampleApp.controller('individ', function($scope, $route) {

    $scope.img =  $route.current.nameHolder;


});

感谢您的任何信息。

Thanks for any info.

更新:
解决方案在新线程。

UPDATED: Solution at new thread.

<一个href=\"http://stackoverflow.com/questions/28416802/angular-js-passing-a-variable-into-routeprovider/28416882#28416882\">angular JS - 传递一个变量到$ routeProvider

推荐答案

您需要将参数添加到您的 $ routeProvider 模板:

You need to add the parameter to your $routeProvider template:

sampleApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/' + nameHolder, {
        templateUrl: 'templates/individkcd/:nameHolder',
        controller: 'individ'
    }).
      when('/', {
        templateUrl: 'templates/home.html'
    });
}]);

然后在你的目标控制器使用 $ routeParams

sampleApp.controller('individ', function($scope, $routeParams) {

    $scope.img =  $routeParams.nameHolder;


});

从上面链接的$ routeProvider文档:

From the $routeProvider docs linked above:

路径可以包含开始用冒号命名组:例如, :名称。所有
  字符,直到达到下一个斜线匹配,并存储在$ routeParams
  以给定名字的时候路由匹配

path can contain named groups starting with a colon: e.g. :name. All characters up to the next slash are matched and stored in $routeParams under the given name when the route matches

这篇关于传递变量到$ routeProvider在视图之间的角度? - angularjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆