1. AngularJS里面使用两个$http请求而且第二个要在第一个执行完之后执行的话,方法如下面例子所示:
1 this.$http.post<IApiResponse<number>>(this.routeConfig.postNewExam(), this.newexam(reviewApproved)) 2 .then(resp => { 3 if (this.newExamOrNot) { 4 this.newExamId = resp.data.Result; 5 this.finalResult = this.newExamId.toString(); 6 } 7 else { 8 this.finalResult = this.selectedExam.Id.toString(); 9 } 10 var ExamID = this.finalResult; 11 return ExamID; 12 }, 13 err => this.errorHandler.apiError(err)) 14 .then(ExamID => { 15 this.$http.get<IApiResponse<any>>(this.routeConfig.exam(ExamID)) 16 .then(resp => { 17 if (!resp.data.Result || resp.data.Result.Id != Number(this.finalResult)) 18 return; 19 if (resp.data.Result.Forms != null) { 20 this.FormList = new Array<string>(this.selectedExamForms); 21 for (var i = 0; i < this.selectedExamForms; i++) { 22 this.FormList[i] = resp.data.Result.Forms[i].key.FormDesc; 23 } 24 } 25 }); 26 }, 27 err => this.errorHandler.apiError(err));
第一个$http请求有两个then,第一个then得到第一个$http请求的result,第二个then则执行第二个$http请求(可以调用第一个$http请求的result)。
2. 如何在不为父子的一个controller和一个directive直接传值(controller传给directive),方法如下面例子所示:
1 export let ddiIframeDirective = function (): ng.IDirective { 2 3 return { 4 restrict: 'AE', 5 scope: { 6 imgPath: '@' 7 }, 8 controllerAs: 'iframeCtrl', 9 controller: ['$sce', '$scope', function ($sce, $scope) { 10 $scope.filter = function () { 11 return $sce.trustAsResourceUrl($scope.imgPath); 12 }; 13 }], 14 template: `<div style='padding-top: 25px'>` + 15 `<iframe height='500' width='450' ng-src='{{filter(imgPath)}}'></iframe>` + 16 `</div>` 17 }; 18 };
<ddi-iframe-directive img-path={{link}}></ddi-iframe-directive>
link为controller内的变量,通过directive内的scope接收到值,然后通过directive内的controller来filter使本地图片文件可以在template显示出来。但是本地文件只能在Edge显示,Chrome因为安全原因禁止显示本地路径的文件,因此要改为IIS上的虚拟地址的文件。
3. Javascript 内如何将string日期转换为date格式,直接用new Date会因为时区问题而不准确,应该把string内的年月日分开来如下例子所示:
var parts ='2014-04-03'.split('-'); // Please pay attention to the month (parts[1]); JavaScript counts months from 0: // January - 0, February - 1, etc. var mydate = new Date(parts[0], parts[1] - 1, parts[2]);