在Angular.js中从另一个控制器调用一个控制器

时间:2013-09-07 12:30:06

标签: javascript angularjs

我无法理解一些基础知识。这是我有两个控制器:

<form ng-controller="NewList" id="form" ng-submit="submit()">
    <input name="title" ng-model="formData.title" placeholder="{{placeholders.title}}" />
    <button type="submit">Generate</button>
    <p ng-show="loading == 1">Loading...</p>
    <p ng-show="loading == 2">Response: {{response}}</p>
</form>

<div id="lists" ng-controller="GetLists">
    <ul>
        <li ng-show="loading">Loading...</li>
        <li class="list" ng-repeat="list in lists">
            <b>{{list.id}}</b> : {{list.title}}
        </li>
    </ul>

</div>

JS:

function NewList($scope, $http) {
    $scope.formData = { };
    $scope.placeholders = { "title" : "List title" };
    $scope.loading = 0;
    $scope.submit = function() {
        $scope.loading = 1;
        $http.post(window.apiBase + 'lists/create', this.formData)
            .success(function(response) {
                $scope.response = response;
                $scope.loading = 2;
            });
    }
}
function GetLists($scope, $http) {
    $scope.loading = true;
    $http.get(window.apiBase + 'lists/all').success(function(response) {
        $scope.lists = response.lists;
        $scope.loading = false
    });
}

我想要实现的目标是提交表单,刷新列表。这意味着,当.success()点击NewList时,请致电GetList并再次发生。我如何实现这一目标?

0 个答案:

没有答案