2015-10-19 48 views
2

我正尝试创建一个angular-ui-bootstrap警报,其中超时值是以编程方式设置的。我阅读了angular-ui docs中的超时解雇属性。我可以为变量设置angular-ui警报超时吗?

这似乎工作:

<uib-alert ng-repeat="alert in alerts" dismiss-on-timeout=5000 type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</uib-alert> 

不过,我能做到以下几点,使用一个变量?它似乎并没有工作:(

<uib-alert ng-repeat="alert in alerts" dismiss-on-timeout="{{alert.timeout}}" type="{{alert.type}}" close="closeAlert($index)" >{{alert.msg}}</uib-alert> 

控制器:

angular.module('ui.bootstrap.demo').controller('AlertDemoCtrl', function ($scope) { 
    $scope.alerts = [ 
    { type: 'danger', timeout: 5000, msg: 'Oh snap! Change a few things up and try submitting again.' }, 
    { type: 'success', timeout: 5000, msg: 'Well done! You successfully read this important alert message.' } 
    ]; 

    $scope.addAlert = function() { 
    $scope.alerts.push({msg: 'Another alert!'}); 
    }; 

    $scope.closeAlert = function(index) { 
    $scope.alerts.splice(index, 1); 
    }; 
}); 
+0

看看你写的东西好像没什么不对。你可以发布控制器和'alert'的定义吗? –

+0

@RicardoVelhote这样做:)它是来自文档的控制器,超时添加到警报。 –

+0

嗯,我认为现在调用“alert.type”还为时过早,因为你和ng-repeat在同一行。也许创建一个全局变量或第二个Controller到你的“body”标签并在那里首先创建一个超时。 –

回答

0

这里,我们去:

  • 它真的象角的UI提示不提供的可能性如果你有兴趣,我已经调试过它并自己做了修改:

ui-bootstrap -tpls-0.14.2.js:

angular.module('ui.bootstrap.alert', []) 

.controller('UibAlertController', ['$scope', '$attrs', '$timeout', function($scope, $attrs, $timeout) { 
    $scope.closeable = !!$attrs.close; 
debugger; 

    if (angular.isDefined($scope.dismissOnTimeout)) { 
    $timeout(function() { 
     $scope.close(); 
    }, parseInt($scope.dismissOnTimeout, 10)); 
    } 
}]) 

.directive('uibAlert', function() { 
    return { 
    controller: 'UibAlertController', 
    controllerAs: 'alert', 
    templateUrl: function(element, attrs) { 
     return attrs.templateUrl || 'template/alert/alert.html'; 
    }, 
    transclude: true, 
    replace: true, 
    scope: { 
     type: '@', 
     close: '&', 
     dismissOnTimeout: '=', 
    }, 
    link: function link(scope, element, attrs) { 
     debugger; 
     console.log("link"); 

    } 
    }; 
}); 

在你的 “app.js” 添加到你的 “AlertController”:

$scope.timeout = 1000; 

而在你的HTML文件:

<uib-alert ng-repeat="alert in alerts" dismiss-on-timeout="timeout" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}, {{alert.timeout}}</uib-alert> 

我已经把angularjs的demo plunkr分叉了,并添加了我自己的angular-ui-bootstrap代码... Here

+1

好的提示。但是你的代码有一个小问题:如果用户会动态改变超时值 - 它将不起作用。看到这个例子:http://plnkr.co/edit/YkUpFveriXntm342Qk17?p=preview。这里用户可以动态改变timeout的值+'dismissOnTimeout'通过''@''参数绑定。 – ababashka

+0

@ababashka你是对的,很快就会更新我的代码,以反映你的解决方案;) –

+0

谢谢你,@ferto和@ababashka!我得到它的工作,非常感谢:) –

相关问题