2016-03-14 20 views

回答

3

我需要将它们设置为工厂,以便我可以从任何控制器调用它们。我发现他们非常有用可能对某人有用。

警报

(function() { 
 
    'use strict'; 
 
    app.factory("showAlert", ["$mdDialog", function ($mdDialog) { 
 
     return function (title, content, ev) { 
 
      $mdDialog.show(
 
       $mdDialog.alert() 
 
       .parent(angular.element(document.querySelector('#popupContainer'))) 
 
       .clickOutsideToClose(true) 
 
       .title(title) 
 
       .textContent(content) 
 
       .ok('Ok') 
 
       .targetEvent(ev)); 
 
     }; 
 
    }]); 
 
})();

  1. 通过使工厂名 'showAlert' 到控制器从任何控制器调用。
  2. 确保您通过HTML中的'$ event',例如NG-点击= “的testAlert($事件)”
  3. 如下调用

app.controller('someController', showAlert) { 
 
    $scope.testAlert = function(event) 
 
    { 
 
     showAlert('Alert Title Goes Here', 'This is the alert message body.', ev); 
 
    } 
 
}


信息助手

(function() { 
 
    'use strict'; 
 
    app.factory("showHelper", ["$mdToast", "$timeout", function ($mdToast, $timeout) { 
 
     return function (content, startTime, duration) { 
 
      $timeout(function() { 
 
       $mdToast.show(
 
        $mdToast.simple() 
 
        .textContent(content) 
 
        .position('bottom left') 
 
        .hideDelay(duration * 1000) 
 
       ); 
 
      }, startTime * 1000); 
 
     }; 
 
    }]); 
 
})();

  1. 通过将工厂名称'showHelper'传递给控制器​​从任何控制器调用。
  2. 传递消息,启动助手的时间和结束助手的时间。
  3. 确保使用一个以上的助手时,下一个帮手计划开始
  4. 我再乘以1000控制器
  5. 古称用秒前以前的助手已经结束如下

app.controller('someController', showHelper) { 
 
\t $scope.testAlert = function() 
 
\t { 
 
\t \t showHelper('I am the first helper', 1, 4); 
 
\t \t showHelper('I am the second helper', 6, 2); 
 
\t } 
 
}

+1

showAlert( '警报名称在这儿', '这是警报消息主体中。',EV);应该是showAlert('Alert Title Goes Here','这是警告消息正文',event); – tfa