小编tom*_*aoq的帖子

为什么.then不是函数?

service.js

.factory('EventService', function ($http, $cordovaSQLite) {
    return {
        //some code here..
        populateData: function (data) {
            var items = [];
            for (i = 0; i < data.length; i++) {
                items.push(data[i]);
            }
            return items;
        }
    }
})
Run Code Online (Sandbox Code Playgroud)

controller.js

.controller('NearCtrl', function ($scope, $http, $cordovaSQLite, EventService) {
    EventService.getDataFromDB().then(function (result) {
        if (result.length > 0) {
            EventService.populateData(result).then(function (items) {
                $scope.items = items;
            })
        } else {
            EventService.getDataFromApi().then(function () {
                EventService.getDataFromDB().then(function (result) {
                    EventService.populateData(result).then(function (items) {
                        $scope.items = items;
                    })
                })
            })
        }
    });
})
Run Code Online (Sandbox Code Playgroud)

当我尝试运行此代码时,我得到"TypeError:EventService.populateData(...).然后不是函数". …

angularjs

14
推荐指数
2
解决办法
5万
查看次数

如何在angularjs表单中添加数组大小验证规则?

我有一个表单,其中包含一些文本输入字段和存储在控制器的$ scope中的项目的动态列表,其中一些函数用于添加/删除列表中的项目.我想使表单无效,直到项目列表达到预定义的长度.

所以我创建了一个formRepeat指令,该指令接受ngModel属性,然后使用ngModelController使表单无效.

http://plnkr.co/edit/jSFvak?p=preview

这有效,但我认为这不是更好的方法,因为指令不是很灵活.

最简单的方法是使控制器中的表单无效:

$scope.myForm.$valid = false;
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

有没有更好的办法 ?

javascript forms validation angularjs angularjs-directive

6
推荐指数
2
解决办法
7361
查看次数