WARNING: Tried to load angular more than once. when I include JQuery

眉间皱痕 提交于 2019-11-27 09:16:17

After long hours of testing... it ended up being that on my index.html file I had a

<ui-view />

to be used by angular ui router and replacing it to this, did the trick.

<ui-view></ui-view>

For me, this happened when I referenced my view incorrectly in my routes.

I had:

.when('/route', {
      templateUrl: 'views/myPage.html',
      controller : 'myCtrl'
})

but my view was named views/mypage.html

The error message is not what I would expect. I would have expected a missing view error.

This happened to me too with .NET and MVC 5 and after a while I realized that within the label: ngview

again included as section scripts happens to you. To solve the problem on the server side what I do is return the partial view. Something like:

public ActionResult Index()
{
    return View();
}

public ActionResult Login()
{
    return PartialView();
}

public ActionResult About()
{
    return PartialView();
}
div

I was getting the same warning and it was because of the order of the files included, as well as the version used.

To resolve the above warning , I re-included the files in the following order:

jquery.js
jqueryui.js
angular.js

Note: You have to add jquery script tag before angularjs so that angularjs can replace jqLite by jQuery.

Moreover, my code was working with AngularJS v1.2.0, but not with a higher angular version. So check for your jquery and angularjs version compatibility as well.

Yes, I solved my problems by sorting the JS script order in html page.

When put the angular js script in the first order, this won't happen any more.

That's very strange.

Thanks.

I was facing the similar issue while working with electron app. I had included angular.min.js in the index.html page and every time I come to that page, the number of times the warning used to appear used to get double..like 1,2,4,8,16 etc. So after 4/5 refresh, app used to get super slow. The solution I found is.. Instead of including angular.min.js with script tag, use require('./path/angular.min.js');

this will load the file only once.

According to node documentation

Caching #

Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.

Multiple calls to require('foo') may not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles.

Here's another scenario that wasn't covered by anyone but it's causing the same problem and it might be not be obvious at first:

If you are using Gulp or any other similar tool with the angular-template-cache plugin and merge all your code into a single file you need to make sure that the templatecache generation is executed before the actual concatenation takes place otherwise your app.min.js won't have the templatecache part.

The Gulpfile looks like this:

var paths = {
  styles: 'assets/less/style.less',
  scripts: 'assets/js/**/*.js',
  templates: 'views/**/*.html'
}

function styles() {
  return gulp.src(paths.styles)
    .pipe(less())
    .pipe(cleanCSS())
    .pipe(gulp.dest('./assets/css/'));
}

function scripts() {
  return gulp.src(paths.scripts, { sourcemaps: true })
    .pipe(babel())
    .pipe(uglify())
    .pipe(concat('app.min.js'))
    .pipe(gulp.dest('./assets/js'));
}

function templatecache() {
  return gulp.src(paths.templates)
    .pipe(templateCache({ module: 'ams', root: '/views/'}))
    .pipe(gulp.dest('./assets/js'));
}

function clean() {
  return del(['assets/js/app.min.js', 'assets/js/templates.js', 'assets/css/style.css']);
};

var build = gulp.series(clean, templatecache, gulp.parallel(styles, scripts));

Key part is the last line, clean and templatecache are executed first then the scripts and styles in parallel. I was executing templatecache in parallel as well and got into the duplicate angularjs warnings. Hope it helps someone.

tools visual studio----just comment the angularjs file like this`

    <script src="~/Scripts/angular.min.js"></script>


</section>`
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!