页面加载两次

时间:2015-03-11 11:45:28

标签: angularjs

页面加载时,警报会触发两次。我期待它只开一次。 这导致在init函数内的数组中插入重复数据。 为什么会这样呢?是否与路由定义有关。如何阻止它。

indexctrl.js:

app.controller("indexctrl", function ($scope, $routeParams) {
var uPId = $routeParams.pqid;
var uCId = $routeParams.cid;

    if (uPId != null && uCId != null) {
        //some other code
    }
    else {
        alert('test');
        init();
    }
     var init = function(){
       //some code
    }
}

app.js:

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

app.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when('/', {
        templateUrl: 'App/views/index.html',
        controller: 'indexctrl'
        })
        .when('/Index', {
            templateUrl: 'App/views/index.html',
            controller: 'indexctrl'
        })
        .when('/Index/pqid/:pqid/cid/:cid', {
            templateUrl: 'App/views/index.html',
            controller: 'indexctrl'
        })            
        .otherwise({
        redirectTo: '/'
        });
}]);

1 个答案:

答案 0 :(得分:2)

1. Check your controller is declared in html file as well.

2.The link which is referring to routes should be like this <a href="#/Index">

3.If your working with another frame works like ionic. it has aleardy angular.js file.so check how many time your loading script file.