Angulajs控制器两次打电话? [英] Angulajs Controller Called Twice?

查看:111
本文介绍了Angulajs控制器两次打电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者angular.js,我已经使用称为从服务器中的数据 ngResource 。不过,我已经想通了,出于某种原因,我的控制器被调用了两次。

这里是我的code;

App.js文件

i am beginner to angular.js and i have called the data from server using ngResource. however, i have figured out that for some reason, my controller is called twice.
Here is my code;
App.js file

// declare a module
var blogApp = angular.module('blogApp', ['ngResource']);

HomeController.js文件;

HomeController.js file;

blogApp.controller("HomeController", function ($scope, $resource) {
    var HotNews = $resource('/api/article/hotnews?culture=en-US');
    $scope.news = HotNews.query();
    alert("hello"); //here i see hello alert box two times
});

我的观点;

  <ul id="news" data-ng-controller="HomeController">
      <li data-ng-repeat="headline in news">
          {{headline.description}}
          <a href="#" title="Read More">» more</a>
      </li>
  </ul>

更新:的问题是 fancybox.js 文件,该文件是在主要布局;

Updated: the issue is in fancybox.js file that is in main layout;

<html data-ng-app="blogApp">

在源文件;

    <!-- JS -->
<script src="/Content/scripts/lib/angular.min.js"></script>
<script src="/Content/scripts/lib/angular-resource.min.js"></script>
<script src="/Content/scripts/app/app.js"></script>
<script src="/Content/scripts/app/controllers/HomeController.js"></script>
@Scripts.Render("~/bundles/jquery")
<script src="/content/fancybox/jquery.fancybox-1.3.4.pack.js"></script>

当我注释掉的fancybox,一切工作正常,但是当我添加回来,我得到两次警告框;

推荐答案

这个问题对我来说最主要的一点,就是脚本被安排在我的项目的方式。所以,我由一个comment-出每个脚本之一,然后我得到的问题。问题是,我是使用所谓的jQuery库 pageslide ,在一个情况下,我有一个按钮,当该用户点击按钮时,页面的滑动左/右;

所以,我只要将我所有的相关文件,所有脚本的结束,现在,我没有看到警报()框两次。

这里要说的是,我的工作安排;

The main point with this issue for me, was the way the scripts were arranged in my project. So, i comment- out each script one by one, and then i got the issue. The issue was, i was using jquery library called pageslide, in a situation where i have a button and when a user click on that button, the page were slide to left/right;
SO, i just move all of my angular related files to the end of all the scripts and now, i don't see the alert() box two times.
here is the arrangement that works for me;

 @Scripts.Render("~/bundles/jquery")
    <script src="/Content/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
    <script src="/content/scripts/lib/general.js"></script>
    <script src="/content/scripts/lib/function.js"></script>
    <script src="/content/scripts/lib/styleswitch.js"></script>
    <!-- elRTE -->
    <script src="/content/scripts/lib/elrte/elrte.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="/Content/scripts/app/dataservice.js"></script>
    <script src="/Content/scripts/app/statemanager.js"></script>

    // this was causing the problem

    <script>
        $("#allpage-login-top").pageSlide({ width: "350px", direction: "left" });
        $("#allpage-signup-top").pageSlide({ width: "350px", direction: "right" });
        $("#allpage-search-top").pageSlide({ width: "350px", direction: "left", modal: true });
        $("#homepage-login-button").pageSlide({ width: "350px", direction: "left" });
        $("#homepage-signup-button").pageSlide({ width: "350px", direction: "right" });
    </script>
        <!-- JS -->
    <script src="/Content/scripts/lib/angular.min.js"></script>
    <script src="/Content/scripts/lib/angular-resource.min.js"></script>
    <script src="/Content/scripts/app/app.js"></script>
    <script src="/Content/scripts/app/controllers/HomeController.js"></script>

    @RenderSection("scripts", required: false)
    @MiniProfiler.RenderIncludes()

这篇关于Angulajs控制器两次打电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆