在控制器中将javascript变量传递给AngularJs中的部分文件

时间:2014-05-09 06:12:08

标签: angularjs angularjs-scope angularjs-routing

我是AngularJs的新手。我在控制器内的scope变量中放了一个url值。该值应该在javascript函数内与控制器对应的部分文件中使用。我怎样才能在AngularJs中实现这一点。

下面给出了代码段以供参考: 配置代码:

myApp.config(function ($routeProvider){
  $routeProvider.
   when('/state-details/:id',
   {
      controller:'controllers.StateDetailsCtrl',
      templateUrl:'/partials/state-details.html'
   })
   .... More routes defined below...

控制器内部:

controllers.StateDetailsCtrl= function($scope, $routeParams, $http){
  $scope.testURL='http://www.google.com'
  .... More values set in the scope..

state-details部分文件中:

<script>
// how to get the value of the testURL defined in the scope?
</script>

请告诉我如何使其正常工作?

1 个答案:

答案 0 :(得分:0)

如果你需要在部分html文件中添加额外的javascript,那就像尖叫一样使用指令。

在那里,您可以继承整个ctrl范围或创建一个独立的范围。

https://docs.angularjs.org/guide/directive