指令的交互与ajax异步出现的问题
2017-11-30 18:59
281 查看
我们知道如果不给指令设置scope得话我们的指令的scope是跟父scope一致的
这个有一个好处
就是如果我们要把一个模型作为指令的属性的参数传递给质量是ok的,所以我们编写公用的指令,不知道父指令用有什么属性的时候就不设置就好了
但是,如果我们父指令中的某个模型是在ajax异步请求以后才完成的,那么因为我们的指令已经渲染成功了,如果我们的这个传入的属性是决定指令渲染的,那么我们的指令的渲染就不会再次改变,导致失效
.directive('star',
function () {
// 星级评分指令
return {
template: "<i class=\"icon ion-ios-star\" ng-repeat=\"
star in stars\"></i>",
// scope:{"="},
link: function ($scope,
element, attrs) {
// 设置star的次数
console.log($scope);
$scope.stars = [];
console.log(attrs.stars);
while (attrs.stars) {
$scope.stars.push({});
attrs.stars--;
}
}
}
})
现在有一个星级评分的指令
如果我们是使用
他的父scope是这样的
unction ($scope,
$http, $ionicLoading) {
$scope.weatherInfo = {};
$ionicLoading.show();
$http.get('http://localhost:8100/views/weather/weather.json').success(function
(data) {
$scope.weatherInfo =
data;
$ionicLoading.hide();
}).error(function () {
console.log("error");
$ionicLoading.hide();
});
如果我们是用
<star
stars="{{weatherInfo.recommendationRate}}"></star>
因为指令的渲染先于ajax
所以最后的时候我们的指令接受到的stars是空的
也就渲染不出来
但是如果是
<star
stars="{{weatherInfo.recommendationRate}}"
ng-if="weatherInfo.recommendationRate"></star>
那么得话
一开始的时候因为是空,所以不渲染,等到ajax结束以后,属性不是空了
就渲染了
因此,问题得到解决
这个有一个好处
就是如果我们要把一个模型作为指令的属性的参数传递给质量是ok的,所以我们编写公用的指令,不知道父指令用有什么属性的时候就不设置就好了
但是,如果我们父指令中的某个模型是在ajax异步请求以后才完成的,那么因为我们的指令已经渲染成功了,如果我们的这个传入的属性是决定指令渲染的,那么我们的指令的渲染就不会再次改变,导致失效
.directive('star',
function () {
// 星级评分指令
return {
template: "<i class=\"icon ion-ios-star\" ng-repeat=\"
star in stars\"></i>",
// scope:{"="},
link: function ($scope,
element, attrs) {
// 设置star的次数
console.log($scope);
$scope.stars = [];
console.log(attrs.stars);
while (attrs.stars) {
$scope.stars.push({});
attrs.stars--;
}
}
}
})
现在有一个星级评分的指令
如果我们是使用
他的父scope是这样的
unction ($scope,
$http, $ionicLoading) {
$scope.weatherInfo = {};
$ionicLoading.show();
$http.get('http://localhost:8100/views/weather/weather.json').success(function
(data) {
$scope.weatherInfo =
data;
$ionicLoading.hide();
}).error(function () {
console.log("error");
$ionicLoading.hide();
});
如果我们是用
<star
stars="{{weatherInfo.recommendationRate}}"></star>
因为指令的渲染先于ajax
所以最后的时候我们的指令接受到的stars是空的
也就渲染不出来
但是如果是
<star
stars="{{weatherInfo.recommendationRate}}"
ng-if="weatherInfo.recommendationRate"></star>
那么得话
一开始的时候因为是空,所以不渲染,等到ajax结束以后,属性不是空了
就渲染了
因此,问题得到解决
相关文章推荐
- [解决]在用AJAX进行异步请求(asp)的时候,出现了乱码问题。
- Ajax 的概念及过程?Ajax 的交互模型?同步和异步的区别?如何解决跨域问题?
- ajax是什么? ajax的交互模型? 同步和异步的区别? 如何解决跨域问题?
- Ajax 是什么?Ajax 的交互模型?同步和异步的区别?如何解决跨域问题?以及 HTTP状态码
- ajax异步请求出现的问题
- ajax是什么? ajax的交互模型? 同步和异步的区别? 如何解决跨域问题?
- 在ajax交互时出现的缓存的问题如何解决?
- ajax异步交互过慢的问题如何转嫁
- 关于客户端与服务器ajax使用json数据交互,出现中文乱码或因包含html标答而报错的问题(base64加密)
- Ajax异步交互解决数据乱码问题
- Ajax异步交互解决数据乱码问题
- jquery+jsp+servlet做$.ajax交互时出现中文乱码(字符集问题)的解决方案
- ajax异步加载的小问题
- Ajax 数据异步交互
- For循环中由于ajax异步导致的问题解决(增加alert数据正常,去掉alert之后数据错误)
- JSP解决ajax使用$.get()方式提交中文出现乱码问题
- 解决ajax异步请求返回的是字符串问题
- JS 实现 ajax 异步浏览器兼容问题
- 解决使用Validform插件,datatype里用ajax验证数据是否已经存在时出现的问题
- web前端页面与后台交互时发生中文参数传递出现乱码问题