最近在研究angularjs的一些Demo。引angularjs包的时候,各种找不到啊,不知道是不是angular是谷歌的原因。
看了一篇博客http://blog.csdn.net/Rongbo_J/article/details/51325606,里面给了一个链接地址:https://code.angularjs.org/。什么版本的angularjs都可以下的。另外引用angularjs和引用JQuery是一样一样的。
注意事项:小编做的Demo是基于C# MVC3.0环境下使用的。如果不是使用MVC的话,在url上的配置就要多费点心了。好办法就行直接把那个类文件拖到你的html页面就可以了。然后稍微改成相对路径。
整体上分为两个步骤
1)前台步骤
使用$http对象
<div ng-app="myApp" ng-controller="siteCtrl">
<ul>
<li ng-repeat="x in names">
{{ x.Name + ', ' + x.Country }}
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('siteCtrl', function($scope, $http) {
$http({
method: 'GET',
url: '/angular/getData'
}).then(function successCallback(response) {
$scope.names = response.data.sites;
}, function errorCallback(response) {
// 请求失败执行代码
});
});
</script>
2)后台读取txt文件
注意读取文件内容时,重载下文件的编码格式,改成Default就可以了。
public string getData()
{
string path = System.AppDomain.CurrentDomain.BaseDirectory + "Content\\data\\dataJson.txt"; //这里是小编txt文件的路径
string text = System.IO.File.ReadAllText(path,System.Text.Encoding.Default); //重载编码格式,汉字占用两个字符
return text;
}
获取路径的一些工具类。最好亲自试一下,方便理解。
string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; 获得当前目录的文件名
//string path = System.IO.Directory.GetCurrentDirectory(); //获得当前目录
//string paths = System.AppDomain.CurrentDomain.BaseDirectory; //获得当前目录+“//”
//string path3 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//string path4 = ".."+System.Windows.Forms.Application.StartupPath; //获得当前目录
文本文件的内容
{"sites": [{"Name": "TGB_Ledary","Url": "www.runoob.com","Country": "CN"},{"Name": "Google","Url": "www.google.com","Country": "USA"},{"Name": "Facebook","Url": "www.facebook.com","Country": "USA" },{"Name": "微博","Country": "CN"}]}