使角度服务正常工作

时间:2016-06-05 12:03:48

标签: angularjs angular-services

关注此reference后,我尝试在角度中制作一个新的(initialized ? identity : initialize) (callback_with_very_long_name) ,用于显示有关用户操作的消息,例如serviceadd,{{1 },edit。我目前所做的是,

update

这是我想在其他控制器中注入的代码的一部分,以便在用户在系统中执行基本delete时获取消息。我有meanApp.factory("flash", function($rootScope) { var queue = [], currentMessage = ''; $rootScope.$on('$locationChangeSuccess', function() { if (queue.length > 0) currentMessage = queue.shift(); else currentMessage = ''; }); return { set: function(message) { queue.push(message); }, get: function(message) { return currentMessage; } }; }); 我想尝试注入此CRUD服务。

postController

现在我想在此控制器中注入flash服务,并希望在用户删除帖子后在bootstrap警报中显示一些消息,如//considering this example which handles the delete operating //delete post meanApp.controller('postDeleteController', function($stateParams, $scope, $http, $location, $state) { $http.delete('/api/posts/' + $stateParams.id).success(function(response) { $location.path('/blog/list'); //refresh(); }); }); 类型的消息。我还需要添加什么才能让消息显示在我的视图中。我在公共布局中使用下面的代码来显示flash

post deleted successfully

1 个答案:

答案 0 :(得分:1)

您可以将您创建的Flash服务注入..

meanApp.controller('postDeleteController', function($stateParams, $scope, $http, $location, $state, flash) {

我不确定您是否可以直接在模板文件中使用 flash 。为此,另一种方法是将值赋给控制器中的变量然后在模板中使用作为..

meanApp.controller('postDeleteController', function($stateParams, $scope, $http, $location, $state, flash) {
$http.delete('/api/posts/' + $stateParams.id).success(function(response) {
    $location.path('/blog/list');
    $scope.value = flash.get();
    //refresh();
});

});

在模板文件中,您可以使用..

 <div class="alert" ng-show="value.length">
        <b>
            Alert!
        </b>
        <br/>
        {{value}}
    </div>