私はAngularが新しく、学習目的で頑丈なtodoアプリケーションを作成しようとしています。おそらく私が間違っている基本的なことがあるかもしれませんが、それを理解することはできません。私はそれが私のコントローラとテンプレートが設定されている方法で何かだと仮定します。私は同様の質問にある推奨事項のいくつかを試しましたが、私の問題を解決するものはありません。私は$scope.$apply()
を使ってみましたが、 "apply/digest already in progress"というエラーが出ます。ngDialogでフォームを送信した後、角度表示が更新されない
誰かが/profile
に行くときにprofileController
を呼び出し、templates/profile.html
にロードするので、ngRouteを使用しています。このコントローラーは実際には現在のユーザーをセットアップして$rootScope
に接続する以外は何もしません。プロファイルビューの内部には、listController
というネストされたコントローラがあります。私はList
リソースをlistController
に渡して、httpリクエストを行います。ルーティングとget/post要求まですべてが正しく機能しています。ここで
は私のコントローラの簡単な例です:
var myApp = angular.module('todo', ['ngRoute', 'ngResource'])
// config stuff...
.controller('listController', ['$scope', 'List', 'ngDialog', function ($scope, List, ngDialog) {
// This correctly returns all lists for the current user
$scope.lists = List.query();
// Open modal form function
$scope.openModal = function() {
ngDialog.open({
template: 'templates/partials/new-list.html',
className: 'ngdialog-theme-default'
});
};
$scope.createList = function() {
List.save({}, {
name: $scope.list.name,
description: $scope.list.description
}).$promise.then(function(result) {
$scope.lists.push(result);
});
};
}]);
ここで正しくList.query()
から取得したすべてのリストを表示するtemplates/profile.html
の関連部分です:
<div class="todo-sidebar" ng-controller="listController">
<h3>Your lists <a href="#" ng-click="openModal()"><span>New list</span></a></h3>
<ul>
<li ng-repeat="list in lists">
<a href="#"><span class="list-name">{{list.name}}</span></a>
</li>
</ul>
</div>
私はcreateList
関数を呼び出すときに問題があります内から私のtemplates/partials/new-list.html
部分:
<div class="dialog-contents" ng-controller="listController">
<h3>New list</h3>
<form ng-submit="createList()">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" ng-model="list.name">
<textarea name="description" ng-model="list.description" rows="10"></textarea>
<button type="submit" class="button">Create list</button>
</div>
</form>
</div>
フォームが正しくポストされ、データベースに新しいリストが表示されますが、ビューはリアルタイムで更新されません。約束の中の私がconsole.log($scope)
のとき、新しいリストがスコープに追加されたようです。
コントローラを複数の要素/テンプレートに接続するのはベストプラクティスではないと感じていますが、それ以外の構造をどのように構成するかはわかりません。
あなたの 'todo-sidebar'と' dialog-contents'は実際に2つのコントローラインスタンスを起動し、 '$ scope'は別々です。 – Shawn
私はいくつかの援助があるかもしれないもののためのドキュメンテーションを調べたので私の答えを見てください。 –