我正在尝试创建一个 Angular 应用程序,它可以在几乎没有指令的情况下进行运输和引导。理想情况下,您应该包含 js 文件:
<script src="myapp.js"></script>
然后在页面上放一个div:
<div id="myDiv"></div>
然后像这样引导应用程序:
angular.bootstrap(document.getElementById('myDiv'),['myApp']);
不用说,这行不通。
见 this fiddle看到它与 ng-* 指令和 this one 一起工作没有。
我想我的问题是 - 这甚至可能吗?
理想情况下,我想实现类似于 Stripe 的应用:
<script
src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button"
data-key="pk_test_czwzkTp2tactuLOEOqbMTRzG"
data-amount="2000"
data-name="Demo Site"
data-description="2 widgets ($20.00)"
data-currency="usd"
data-image="/128x128.png">
</script>
但也找不到让它工作的方法。主要目标是不必暴露 Angular 细节来使用我的应用程序。
最佳答案
来自官方文档:
If you need to have more control over the initialization process, you can use a manual bootstrapping method instead. You should not use the ng-app directive when manually bootstrapping your app.
<!doctype html>
<html>
<body>
<div ng-controller="MyController">
Hello {{greetMe}}!
</div>
<script src="http://code.angularjs.org/snapshot/angular.js"></script>
<script>
angular.module('myApp', [])
.controller('MyController', ['$scope', function ($scope) {
$scope.greetMe = 'World';
}]);
angular.element(document).ready(function() {
angular.bootstrap(document, ['myApp']);
});
</script>
</body>
</html>
关于javascript - angular.bootstrap() - 在不需要 ng-app 或 ng-controller 的情况下引导 Angular 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17730159/