繁体   English   中英

Angular 2nd指令不起作用

[英]Angular 2nd directive not working

我在一页中有2条指令。 第一个工作正常,但第二个工作不正常。

这是我的代码:

的HTML

<body class="login" ng-app="Login">
<div ng-controller="HttpLoginController">
<wrongdetails></wrongdetails>
<loading></loading>
<input type="submit" ng-click="LoginUser()" value="Login" />
</div>
</body>

JS

var app = angular.module("Login", [])
app.controller("HttpLoginController", function($scope,$http){
$scope.LoginUser = function(){
$scope.loading = true;
var data = [];
var config = {}
$http.post('Mylink', data, config)
.success(function (data, status, headers, config) { $scope.loading = false;})
.error(function (data, status, header, config) {$scope.wrongdetails = true; });
};      
});
//Directives
app.directive('loading', function () {
    return {
        restrict: 'E',
        //replace:true,
        template: '<div id="loading"> <div class="progress-line"></div><br/> </div>',
        link: function (scope, element, attr) {
              scope.$watch('loading', function (val) {
                  if (val)
                      $(element).show();
                  else
                      $(element).hide();
              });
        }
    }
})
app.directive('wrongdetails', function () {
    return {
        restrict: 'E',
        replace:true,
        template: '<div class="alert alert-danger display-hide"><button class="close" data-close="alert"></button><span> Error. </span></div>',
        link: function (scope, element, attr) {
              scope.$watch('wrongdetails', function (val) {
                  if (val)
                      $(element).show();
                  else
                      $(element).hide();
              });
        }
    }
})

但是第二指令从不显示。 我做错了什么?

对不起,我错了。 我忘了复制粘贴指令。 我想现在还可以

在第二个指令中删除以下行:

replace:true ,

带有说明的原因已经存在。 请参考此链接Angular Directives中的Explain replace = true(已弃用)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM