首页 > 代码库 > [AngularJS] Accessing Data in HTML -- controllerAs, using promises
[AngularJS] Accessing Data in HTML -- controllerAs, using promises
<!DOCTYPE html><html><head> <title>Access Data From HTML</title></head><body ng-app="app" ng-controller="TodoCtrl as todoCtrl"><div ng-repeat="todo in todoCtrl.todos"> {{todo.item}}</div><form> <input type="text" ng-model="newTodo"/> <input type="submit" ng-click="todoCtrl.addTodo(newTodo)"/></form><script src="bower_components/angular/angular.js"></script><script src="app.js"></script></body></html>
/** * Created by Answer1215 on 11/29/2014. */function ToDoServices($q, $timeout) { var ToDoServices = {}; ToDoServices.getTodos = function() { return $q(function(resolve, reject) { $timeout(function() { resolve( { todos: [ {item: "Clean room", done: false}, {item: "Eat lunch", done: false}, {item: "Wash car", done: false} ] } ) }, 500); }); }; ToDoServices.addTodo = function(item) { this.todos.push({item: item, done: false}) } return ToDoServices;}function TodoCtrl(ToDoServices) { var vm = this; vm.todos = []; vm.getTodos = ToDoServices.getTodos; vm.addTodo = ToDoServices.addTodo; vm.getTodos(vm.isReject).then(function(res) { vm.todos = res.todos; });}angular.module("app", []) .factory("ToDoServices", ToDoServices) .controller("TodoCtrl", TodoCtrl);
[AngularJS] Accessing Data in HTML -- controllerAs, using promises
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。