Angularjs 角度-2+;在同一html中生成应用程序指令
我需要添加一个带有函数的控制器和另一个函数,用[[(我需要使用Jekyll)替换{ 我有两个ng应用程序:Angularjs 角度-2+;在同一html中生成应用程序指令,angularjs,angularjs-controller,Angularjs,Angularjs Controller,我需要添加一个带有函数的控制器和另一个函数,用[[(我需要使用Jekyll)替换{ 我有两个ng应用程序: myApp 发票 两个文件: myApp.js invoice.js 每个文件的以下内容: myApp.JS var myApp = angular.module('myApp', []); myApp.config(function($interpolateProvider) { $interpolateProvider.startSymbol('[['); $i
- myApp
- 发票
- myApp.js
- invoice.js
var myApp = angular.module('myApp', []);
myApp.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
});
HTML myApp:
<div ng-app="myApp" ng-init="qty=1;cost=2">
<b>Invoice:</b>
<div>
Quantity: <input class="form-control" type="number" min="0" ng-model="qty">
</div>
<div>
Costs: <input class="form-control" type="number" min="0" ng-model="cost">
</div>
<div>
<b>
Total:
</b> [[qty * cost | currency]]
</div>
</div>
<div ng-app="invoice1" ng-controller="InvoiceController as invoice">
<form class="form-horizontal" id="frm_basic">
<b>Invoice:</b>
<div>
Quantity: <input type="number" min="0" ng-model="invoice.qty" required >
</div>
<div>
Costs: <input type="number" min="0" ng-model="invoice.cost" required >
<select ng-model="invoice.inCurr">
<option ng-repeat="c in invoice.currencies">[[c]]</option>
</select>
</div>
<div>
<b>Total:</b>
<span ng-repeat="c in invoice.currencies">
[[invoice.total(c) | currency:c]]
</span>
<button class="btn" ng-click="invoice.pay()">Pay</button>
</div>
</form>
</div>
它似乎工作不正常。它没有用[]替换{。
错在哪里?
当我同时使用页面中的html组件(myApp和invoice)时会出现错误。如果禁用“myApp”,第二个组件将正常工作
已解决
同一html中不能有超过1个AngularJS实例。AngularJS应用程序不能相互嵌套。根据需要,您必须使用invoic1.config(..
这个例子很好用-为你的代码提供一个plunker,这样我们可以帮助你
var custom插值App=angular.module('App',[]);
customInterpolationApp.config(函数($InterpolationProvider){
$interpolateProvider.startSymbol('/');
$interpolateProvider.endSymbol('/');
});
customInterpolationApp.controller('DemoController',函数(){
this.label=“此绑定由//插值符号提供。”;
});
//demo.label//
您不想将控制器属性和函数添加到$scope而不是这个吗?这可能只是一个输入错误,但是当您要求$interpolateProvider
使用[
作为分隔符时,您说您想使用[
。对不起,我想说[[但它不起作用。我试过了,但不起作用。我在原始帖子中添加了代码。
var invoice1 = angular.module('invoice1', [], function($interpolateProvider) {
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
});
invoice1.controller('InvoiceController', function() {
this.qty = 1;
this.cost = 2;
this.inCurr = 'EUR';
this.currencies = ['USD', 'EUR', 'CNY'];
this.usdToForeignRates = {
USD: 1,
EUR: 0.74,
CNY: 6.09
};
this.total = function total(outCurr) {
return this.convertCurrency(this.qty * this.cost, this.inCurr, outCurr);
};
this.convertCurrency = function convertCurrency(amount, inCurr, outCurr) {
return amount * this.usdToForeignRates[outCurr] / this.usdToForeignRates[inCurr];
};
this.pay = function pay() {
window.alert("Thanks!");
};
});