配置路由时,使用href调用angular js函数

时间:2015-08-29 15:24:57

标签: angularjs

使用ng-click调用时,函数正在调用。但是对于我要求为路由更改(URL)调用该函数的要求,我已经用' href'但它不起作用。对此的指导。语法如下;



<!--VIEW-->

This call to controller was successfully
  < a class = "file"
data - ng - click = "vm.getDevices(true,'byLocation')" > Link One < /a>

If this URL captured, initialization function in the controller will be called indirectly
<a class="file" href="#/country/india/city/chennai ">Link Two</a>


//controller
(function () {
    'use strict';
	 var controllerId = 'testController';

    angular
        .module('app')
        .controller(controllerId, ['$q', '$scope', '$location', '$routeParams']);

    function EsdEntryController($q, $scope, $location, $routeParams) {
	init();
	
	var vm = this
            , security = common.security
            , sortOrder;
			
    //page init functionality
    function init() {
		//route configured as below
		//url: '/country/:country/city/:city/'
		//
		if($routeParams.country=='india' && $routeParams.city=='chennai'){
			//calls to our function
			testFunction(param1, param2);//this call is not invoking properly 
		}
	}
      
	vm.testFunction = function (param1, param2) {
            getDevices(val1, val2');
        }

	function testFunction(param1, param2) {
			//function implementation here
		}
)();
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

$ routeParams要求您注入ngRoute服务。

而不是

 angular.module('app')

写下这个:

angular.module('app', ['ngRoute'])