简体   繁体   English

如何使用angularjs路由来调用javascript函数

[英]How to use a angularjs route to call a javascript function

I'm trying to use the routing of angularjs to call a javascript function if a certain url is used. 我正在尝试使用angularjs的路由来调用javascript函数,如果使用某个url。

The following code is not providing the expected result: 以下代码未提供预期结果:

var app = angular.module('myApp', []);

app.config(function($routeProvider) {
    $routeProvider.when('/link1', {
         controller: 'PageController'
   })
   .when('/link2', {
        controller: 'PageController'
   })
   .otherwise({ 
        controller: 'PageController'
   });
});

app.controller('PageController', function($scope, $routeParams) {
   alert('1');
});

The alert(1); alert(1); is not called if one of these URLs are requested... 如果请求其中一个URL,则不会调用...

Maybe someone knows how to solve this ? 也许有人知道如何解决这个问题?

Controller is not called until you specify template or templateUrl option in $routeProvider configuration. 在$ routeProvider配置中指定template或templateUrl选项之前,不会调用Controller。 If there is no template needed, you could specify one-space char (but not empty string). 如果不需要模板,则可以指定一个空格字符(但不是空字符串)。 Like so 像这样

$routeProvider.when('/link1', {
     controller: 'PageController',
     template: ' '
})

There is no way to associate the routing with a specific action in the controller. 无法将路由与控制器中的特定操作相关联。 The routing in the AngularJS is not like the routing in other web frameworks to route to specific action of request. AngularJS中的路由与其他Web框架中的路由不同,以路由到请求的特定操作。 Instead, the routing in the AngularJS is primarily relating to handle the page flow and the controller defines the scope of the page. 相反,AngularJS中的路由主要与处理页面流有关,控制器定义页面的范围。

However, if you put the alert in the controller like that, it should be triggered when the page is loaded. 但是,如果您将警报放在控制器中,则应在页面加载时触发警报。 You need to check whether the URL you used is correct or not. 您需要检查您使用的URL是否正确。 To test, you can simply put $location.url('/link1') in your code. 要进行测试,您只需将$location.url('/link1')放入代码即可。

If your controller is being used on a particular route, then you can call that function inside the controller. 如果您的控制器正在特定路径上使用,则可以在控制器内调用该功能。 It will get executed once the route changes and your controller is called. 一旦路线改变并且你的控制器被调用,它就会被执行。

In this http://plnkr.co/edit/qUZ5Q7nKCRAS8dFvjRIg when you click on link1 it displays alert. 在此http://plnkr.co/edit/qUZ5Q7nKCRAS8dFvjRIg中,当您单击link1时,它会显示警报。

I can't quite catch why your code doesn't work as expected, but I created a similar app setup and it works: 我无法理解为什么你的代码没有按预期工作,但我创建了一个类似的应用程序设置,它的工作原理:

var app = angular.module('myApp',[]).

config(['$routeProvider',function($routeProvider) {
      $routeProvider.
          when('/', {
            controller: 'PageController',
            template: '<br><br>this is page #/<br> {{data}}',
          }).
          when('/link1', {
            controller: 'SpecificPageController',
            template: '<br><br>this is page #/link1<br> {{data}}' 
          }).
          when('/link2', { 
            controller: 'PageController',
            template: '<br><br>this is page #/link2<br> {{data}}' 
          }).
          otherwise({redirectTo:'/'});
    }]).

controller('PageController', function($scope, $routeParams) {
    $scope.data = 'hello world';
}).

controller('SpecificPageController', function($scope, $routeParams) {

    $scope.data = 'hello specific';
    alert(1);    
});

Whenever SpecificPageController is assigned to a route, and that route opened, the alert function gets executed. 每当将SpecificPageController分配给路由并打开该路由时,都会执行alert函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM