C#angularjs读取后台数据

    最近在研究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"}]}


略有偏颇之处,还请指正。关于文件读写这方面的内容,还请大家多多指点。比如Excel,Word,xml的文件流的读写等等。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 29
    评论
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值