ajax和angular效率,angular指令和Ajax调用之间的时间问题

return 'x';不要手动绑定这些内容,而是在模板中执行此操作。

{{value}}
。使用观察程序监听propertyMap上的更改,并在其更改后立即应用其他逻辑(通过ajax加载)$scope.$watch()。如果你做的一切正确,你的模板将自动更新。 (如果你没有使用angular的$ http服务,你可能必须使用$ scope。$ apply()。

这是一个如何用angular(代码未测试)写这个的例子

js部分:

angular.module('stackoverflow', [])

.controller('questionsCtrl', function($scope, $http) {

$scope.questions = null;

$http.get('stackoverflow.com/questions').then(function(questions) {

$scope.questions = questions;

});

})

.directive('questionsList', {

restrict: 'EA',

templateUrl: 'directives/questionsList.html',

scope: {

questions: '=',

},

controller: function($scope) {

$scope.$watch('questions', function(newValue, oldValue) {

if (newValue !== null) console.log('all questions loaded');

});

}

})

和html:

  • {{question.title}}

如果您是通过ajax加载html并希望在页面上呈现它(具有角度功能),请考虑使用ng-include指令。否则,您必须使用$compile服务自行编译html:

// example of how to compile html in your controller

// consider thou that $compiling or modifying html from within your controller is considered as bad practice

.controller('someCtrl', function($scope, $element, $compile, $timeout) {

var scope = $scope.$new();

scope.question = {title: 'angular stuff'};

$element.append(

$compile('

{{question.title}}
')(scope)

);

// will update your html in 2 seconds

$timeout(function() {

scope.question.title = 'javascript stuff';

}, 2000);

});

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值