如何从ng-include AngularJs中指定的url中检索查询字符串参数

时间:2014-07-01 18:28:29

标签: javascript angularjs

我有一个转发器div,让我们说

<div ng-repeat =" employee in employees">
     <div data-ng-include="'./app/details/employeedetails.html?id=123'"></div>
//id will change based on employee.
</div>

这将为员工中的每位员工调用员工详细信息控制器。 我需要获取查询字符串参数并检索该员工的详细信息。 如果它在路由中,那么我可以使用$ location.search()但这不会导致路由更改。

1 个答案:

答案 0 :(得分:1)

正如Dayan Moreno Leon所说,您可以使用ngInit初始化本地属性,然后在ngInclude d模板中引用该属性:

<!-- The VIEW -->
<div ng-repeat="employee in employees" ng-init="id=employee.id">
    <div ng-include="'app/details/employeedetails.html'"></div>
</div>

<!-- The TEMPLATE -->
Employee ID: {{id}}

另请参阅此 short demo