Javascript AngularJS,如何在外部JS中使用变量<;脚本>;在HTML中?
我不是AngularJS专家,我有一个简单的问题 我的代码在页面开头的中定义了应用程序和控制器:Javascript AngularJS,如何在外部JS中使用变量<;脚本>;在HTML中?,javascript,html,angularjs,Javascript,Html,Angularjs,我不是AngularJS专家,我有一个简单的问题 我的代码在页面开头的中定义了应用程序和控制器: <script> var isisApp = angular.module('isisApp', []); isisApp.controller('AccountListCtrl', function($scope) { $scope.sample = {'firstName': 'John', 'lastName': 'Smith', 'age': 2
<script>
var isisApp = angular.module('isisApp', []);
isisApp.controller('AccountListCtrl', function($scope) {
$scope.sample = {'firstName': 'John', 'lastName': 'Smith', 'age': 25, 'AccountType': 'Safekeeping',
'address': {'street': 'Dorsstok', 'city': 'Erica'},
'phone': [{'type': 'home', 'number': '+31591302900'}, {'type': 'mobile', 'number': '+31625299740'}, {'type': 'Business', 'number': '+43223627551'}],
'account': [{'type': 'Bonds', 'value': '111111'}, {'type': 'Stocks', 'value': '$ 0625299740'}, {'type': 'Funds', 'value': '999990'}],
'json_read_date': '4/12'
};
});
</script>
我得到这个错误:“未定义”不是一个函数等
你知道吗?我在body标记中使用ng controller=“AccountListCtrl”,我的第二个脚本也是
提前谢谢!
法比奥
在Isha的回答之后,我修改了代码,但我看不到任何图表:
<!-- Graph !-->
<canvas id="canvas" height="450" width="450"></canvas>
<script>
isisApp.controller('ChartJS', function($scope, Init) {
$scope.init = Init;
var delivery = $scope.init.json_doughnut_delivery;
var taxother = $scope.init.json_doughnut_taxother;
var supply = $scope.init.json_doughnut_supply;
var doughnutData = [
{
value: delivery,
color: "#000000"
},
{
value: taxother,
color: "#838383"
},
{
value: supply,
color: "#FFFF00"
}
];
var myDoughnut = new Chart(document.getElementById("canvas").getContext("2d")).Doughnut(doughnutData);
});
</script>
控制器('ChartJS',函数($scope,Init){
$scope.init=init;
var delivery=$scope.init.json_doughnut_delivery;
var taxother=$scope.init.json_doughnut_taxother;
var supply=$scope.init.json_doughnut_supply;
var doughnutData=[
{
价值:交付,
颜色:“000000”
},
{
价值:其他税,
颜色:#838383“
},
{
价值:供应,
颜色:“FFFF00”
}
];
var myDoughnut=新图表(document.getElementById(“canvas”).getContext(“2d”)).Doughnut(doughnutData);
});
在它仅仅与以下方面合作之前:
<!-- Graph !-->
<canvas id="canvas" height="450" width="450"></canvas>
<script>
var doughnutData = [
{
value: 32,
color: "#000000"
},
{
value: 23,
color: "#838383"
},
{
value: 11,
color: "#FFFF00"
}
];
var myDoughnut = new Chart(document.getElementById("canvas").getContext("2d")).Doughnut(doughnutData);
</script>
var doughnutData=[
{
数值:32,
颜色:“000000”
},
{
价值:23,
颜色:#838383“
},
{
价值:11,
颜色:“FFFF00”
}
];
var myDoughnut=新图表(document.getElementById(“canvas”).getContext(“2d”)).Doughnut(doughnutData);
您可以使用工厂
提供样本
数据,并将其注入其他控制器、指令
isisApp.factory('Sample', function(){
return {'firstName': 'John', 'lastName': 'Smith', 'age': 25, 'AccountType': 'Safekeeping',
'address': {'street': 'Dorsstok', 'city': 'Erica'},
'phone': [{'type': 'home', 'number': '+31591302900'}, {'type': 'mobile', 'number': '+31625299740'}, {'type': 'Business', 'number': '+43223627551'}],
'account': [{'type': 'Bonds', 'value': '111111'}, {'type': 'Stocks', 'value': '$ 0625299740'}, {'type': 'Funds', 'value': '999990'}],
'json_read_date': '4/12'
};
});
isisApp.controller('AccountListCtrl', function($scope, Sample){
$scope.sample = Sample;
//now you have an access 'json_read_date' inside this controller, via - Sample.json_read_date OR $scope.sample.json_read_date
});
isisApp.controller('ChartJS', function($scope, Sample){
$scope.sample = Sample;
//now you have an access 'json_read_date' inside this controller, via - Sample.json_read_date OR $scope.sample.json_read_date
});
希望有帮助。你好,伊莎,非常感谢你的帮助。我继续做了,但没有成功地创建图表:我下面的答案中的代码不起作用。。
isisApp.factory('Sample', function(){
return {'firstName': 'John', 'lastName': 'Smith', 'age': 25, 'AccountType': 'Safekeeping',
'address': {'street': 'Dorsstok', 'city': 'Erica'},
'phone': [{'type': 'home', 'number': '+31591302900'}, {'type': 'mobile', 'number': '+31625299740'}, {'type': 'Business', 'number': '+43223627551'}],
'account': [{'type': 'Bonds', 'value': '111111'}, {'type': 'Stocks', 'value': '$ 0625299740'}, {'type': 'Funds', 'value': '999990'}],
'json_read_date': '4/12'
};
});
isisApp.controller('AccountListCtrl', function($scope, Sample){
$scope.sample = Sample;
//now you have an access 'json_read_date' inside this controller, via - Sample.json_read_date OR $scope.sample.json_read_date
});
isisApp.controller('ChartJS', function($scope, Sample){
$scope.sample = Sample;
//now you have an access 'json_read_date' inside this controller, via - Sample.json_read_date OR $scope.sample.json_read_date
});