AngularJS - 将值从独立作用域指令传递到另一个元素
AngularJS - Pass value from isolated scope directive to another element
有没有一种方法可以将布尔值 (dataLoading) 从独立的作用域指令传递到另一个元素,以在登录操作期间加载数据时显示进度条?
index.html
<div class="progress-line" ng-show="dataLoading"></div>
<login-user email = "email" password = "password" show-nav-bar="showNavBar" recover-password="recoverPassword(email)" data-loading="dataLoading"></login-user>
login.component.js
angular
.module('login')
.directive('loginUser', [function() {
return {
scope: {
email: '=email',
password: '=password',
showNavBar: '=showNavBar',
dataLoading: '=dataLoading',
recoverPassword: '&'
},
controller: 'LoginController',
templateUrl: 'login/login.template.html',
};
}]);
login.controller.js
function recoverPassword(email) {
console.log("recoverPassword email: " + email);
$scope.dataLoading = true;
authenticationService.recoverPassword(email, function (response) {
console.log("Response success, status: " + angular.toJson(response.data.status) + " message: " + angular.toJson(response.data.message));
$scope.message = response.data.message;
$scope.dataLoading = false;
});
}
您可以使用 EventEmitter 将数据发送到您的控制器。
EventEmitter({loading: true })
在您的组件上添加绑定以捕获更改:
onDataChanges: '&'
然后您在组件控制器上捕获数据:
callbackdata(data) {
this.loading= data.loading;
}
你的观点:
<login ... onDataChanges="callbackdata($event)" ...></login>
是的,绝对是。您可以创建一个公开 say 'loading' 属性 的服务,然后将该服务注入您的控制器和指令。在您的指令中设置您的服务的 'loading' 属性。这就是你的指令。
现在将您注入到指令中的相同服务注入到元素的控制器中。因此,如果该服务名为 myloadingservice,您将执行类似 $scope.myloadingservice = myloadingservice 的操作。一旦你有了,你就可以直接绑定到元素中服务的加载 属性,就像这样:ng-show="myloadingservice.loading"
希望对您有所帮助。
通常让数据单向流动是更好的做法。
因此,dataLoading
变量的所有者应该是两个组件的共同父级。
如果该布尔值的逻辑应该在 user-login
组件内部,您可以向它传递一个回调(通过 &
),然后 user-login
组件将在启动时调用它获取数据,然后父级将更改该布尔值,并将其传播给相关的子级。
const app = angular.module('app', []);
app.directive('parentComponent', function() {
return {
controllerAs: 'parentVM',
controller: function() {
this.isLoading = false;
this.onLogin = () => {
this.isLoading = true;
}
},
template: '<div><child on-login="parentVM.onLogin()"></child><other-child is-loading="parentVM.isLoading"></other-child></div>'
};
})
.directive('otherChild', function() {
return {
scope: {
localLoading: '=isLoading'
},
template: '<div ng-class="{\'is-loading\':localLoading}">{{localLoading? \'Loading...\': \'\'}}</div>'
}
})
.directive('child', function() {
return {
scope: {
onLogin: '&'
},
template: '<button ng-click="onLogin()">Login</button>'
};
})
.is-loading {
background-color: rgba(0, 200, 0, 0.2);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.min.js"></script>
<div ng-app="app">
<parent-component></parent-component>
</div>
有没有一种方法可以将布尔值 (dataLoading) 从独立的作用域指令传递到另一个元素,以在登录操作期间加载数据时显示进度条?
index.html
<div class="progress-line" ng-show="dataLoading"></div>
<login-user email = "email" password = "password" show-nav-bar="showNavBar" recover-password="recoverPassword(email)" data-loading="dataLoading"></login-user>
login.component.js
angular
.module('login')
.directive('loginUser', [function() {
return {
scope: {
email: '=email',
password: '=password',
showNavBar: '=showNavBar',
dataLoading: '=dataLoading',
recoverPassword: '&'
},
controller: 'LoginController',
templateUrl: 'login/login.template.html',
};
}]);
login.controller.js
function recoverPassword(email) {
console.log("recoverPassword email: " + email);
$scope.dataLoading = true;
authenticationService.recoverPassword(email, function (response) {
console.log("Response success, status: " + angular.toJson(response.data.status) + " message: " + angular.toJson(response.data.message));
$scope.message = response.data.message;
$scope.dataLoading = false;
});
}
您可以使用 EventEmitter 将数据发送到您的控制器。
EventEmitter({loading: true })
在您的组件上添加绑定以捕获更改:
onDataChanges: '&'
然后您在组件控制器上捕获数据:
callbackdata(data) {
this.loading= data.loading;
}
你的观点:
<login ... onDataChanges="callbackdata($event)" ...></login>
是的,绝对是。您可以创建一个公开 say 'loading' 属性 的服务,然后将该服务注入您的控制器和指令。在您的指令中设置您的服务的 'loading' 属性。这就是你的指令。 现在将您注入到指令中的相同服务注入到元素的控制器中。因此,如果该服务名为 myloadingservice,您将执行类似 $scope.myloadingservice = myloadingservice 的操作。一旦你有了,你就可以直接绑定到元素中服务的加载 属性,就像这样:ng-show="myloadingservice.loading"
希望对您有所帮助。
通常让数据单向流动是更好的做法。
因此,dataLoading
变量的所有者应该是两个组件的共同父级。
如果该布尔值的逻辑应该在 user-login
组件内部,您可以向它传递一个回调(通过 &
),然后 user-login
组件将在启动时调用它获取数据,然后父级将更改该布尔值,并将其传播给相关的子级。
const app = angular.module('app', []);
app.directive('parentComponent', function() {
return {
controllerAs: 'parentVM',
controller: function() {
this.isLoading = false;
this.onLogin = () => {
this.isLoading = true;
}
},
template: '<div><child on-login="parentVM.onLogin()"></child><other-child is-loading="parentVM.isLoading"></other-child></div>'
};
})
.directive('otherChild', function() {
return {
scope: {
localLoading: '=isLoading'
},
template: '<div ng-class="{\'is-loading\':localLoading}">{{localLoading? \'Loading...\': \'\'}}</div>'
}
})
.directive('child', function() {
return {
scope: {
onLogin: '&'
},
template: '<button ng-click="onLogin()">Login</button>'
};
})
.is-loading {
background-color: rgba(0, 200, 0, 0.2);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.min.js"></script>
<div ng-app="app">
<parent-component></parent-component>
</div>