简体   繁体   English

Angular控制器被调用两次

[英]Angular controller getting called twice

I've seen this issue asked however, I'm a little stumped with mine. 我已经看到了这个问题,但是,我对我有些困惑。 Solutions state to remove one of the controllers. 解决方案指出要删除其中一个控制器。 So when I remove the ng-controller=sigSearchResults from the body tag, my results do not get populated in the ng-grid="gridOptions" . 因此,当我从body标签中删除ng-controller = sigSearchResults时,我的结果不会填充在ng-grid =“ gridOptions”中。 How do rectify this? 该如何纠正?

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

    sig.config([ '$routeProvider', function($routeProvider) {
        $routeProvider.when('/search/:sig', {
             template : ' ',
             controller : 'sigSearchResults'
        }).when('/', {
             template : ' ',
             controller : 'sigSearchResults'
        }).otherwise({
            redirectTo : '/'
        });
    } ]);

sig.controller('sigSearchResults', function($scope,$log,$http,$q,$routeParams, $window) {
    console.log($routeParams);
    //code that populates ng-grid to gridOptions element.
});


<html ng-app="sig">    
<body ng-controller="sigSearchResults">
<div class="contentContainer">

    <div ng-view></div>

    <!-- This is the template for the result -->
    <script type="text/ng-template" id="sig-search-result-id" >

    <div ng-show="loading" style='clear:both; text-align: center;'>Loading report...<img src="../media/images/Wait3.gif"></div>


    <div ng-show="!loading" class="gridStyle" ng-grid="gridOptions"></div>
    </script>

</div>

Yes, remove the ng-controller="sigSearchResults" from your <body> tag as you're using the $routeProvider to configure routing and views and it doesn't look like your controller should be running at the top level. 是的,当您使用$routeProvider配置路由和视图时,请从<body>标记中删除ng-controller="sigSearchResults" ,并且您的控制器似乎不应在顶层运行。

Looks like all you're missing is the tempateUrl in your route configuration, ie 看起来您所缺少的tempateUrl您的路由配置中的tempateUrl ,即

when('/', {
     templateUrl: 'sig-search-result-id',
     controller: 'sigSearchResults'
})

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

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