手动自举AngularJS,然后让模块 [英] Manually bootstrapping AngularJS and then getting the module

查看:75
本文介绍了手动自举AngularJS,然后让模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般情况下,我会做以下,并会出现在我的HTML是 NG-应用

Generally, I'd do the following and there would be an ng-app in my HTML:

var myApp = angular.module("myApp", []);

myApp.controller("AttributeCtrl", function ($scope) {
    $scope.master = {
        name: "some name"
    };
});

不过,我需要因为我只有在通过jQuery的载入我的应用程序的一部分,用它来引导手动角 $。负载()。如果我做了以下内容:

However, I need to manually bootstrap angular because I'm only using it in a part of my app that is loaded via jQuery's $.load(). If I do the following:

main.js - 这是我想对在被拉动角的页面

main.js - this is where the page I want to use angular on is being pulled in

$("#form").load(contextPath + url, params, function() {
    angular.bootstrap($("#angularApp"));
});

和则该页面已被拉到它自己的JavaScript:

And then the page being pulled in has it's own javascript:

function AttributeCtrl($scope) {
  $scope.master = { name: "some name"};
}

这个作品,但是,理想情况下,我想在模块级先限定我的控制器。所以,我修改了上面的code像这样

This works, however, ideally, I'd like my controllers to be scoped at the module level. So I modified the above code like so

main.js

$("#form").load(contextPath + url, params, function() {
    angular.bootstrap($("#angularApp", ["myApp"]));
});

然后...

var app = angular.module("myApp"); // retrieve a module

app.controller("AttributeCtrl", function($scope) {
  $scope.master = { name: "some name"};
});

检索这种方式似乎并没有工作,虽然该模块。难道我做错了什么?

Retrieving the module this way doesn't seem to work, though. Am I doing something wrong?

推荐答案

您已经自举应用程序后,您不能创建一个控制器。请参见 angular.bootstrap 文档rel=\"nofollow\">。

You cannot create a controller after you've bootstrapped the app. See the documentation for angular.bootstrap.

您应该调用angular.bootstrap()已加载或定义你的模块后。一个应用程序后,白手起家您不能添加控制器,服务指令等。

You should call angular.bootstrap() after you've loaded or defined your modules. You cannot add controllers, services, directives, etc after an application bootstraps.

这篇关于手动自举AngularJS,然后让模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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