【发布时间】:2019-05-29 22:40:16
【问题描述】:
以下是一个模板,它的工作原理是 webix-ui 指令指向子作用域,因为它位于 ng-if 内...
在初始化 ui 组件配置对象之前,我需要 ng-if 等待 $scope.init() 完成获取数据。
如何在我的 webix-ui 指令中指向范围,而不是子范围?
<div id="formcontainer" ng-app="Risk" ng-controller="CreateRiskController" get-risk>
<div id="mycreateform" class="container" type="space" layout-padding="">
<form id="form" name="CreateRisk" role="form" ng-submit="valid() && submit()" novalidate>
<div id="details" class="layout table" border="1">
<div class="tr">
<div class="td label">
Title
</div>
<div class="td locked">
<div ng-if="initDone">
<div webix-ui="config.risktitle" width="500"
height="30" type="text" id="risktitle"
name="risktitle">
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
这正确地指向范围,因为它没有嵌套在 ng-if 中
angular.module('Risk').directive('getRisk', getRisk);
function getRisk(){
return {
restrict: 'A',
controller: function ($scope, $http, $sce){
$scope.risklevels = {
riskmaximum: '',
riskhigh: '',
riskmedium: '',
riskminimum: ''
}
$scope.riskMatrix = [];
for(var l = 1; l <= 5; l++)
{
$scope.riskMatrix[l] = [];
for (var c = 0; c <= 5; c++)
{
$scope.riskMatrix[l][c] = '';
}
}
$scope.initDone = false;
$scope.init = function(){
angular.element(document.querySelector('link[href="/app/tool/risk/CreateRisk.css"]')).remove();
angular.element(document.querySelector('head')).append('<link type="text/css" rel="stylesheet" href="/app/tool/risk/CreateRisk.css"/>');
return $http.get('/api/riskconfig').then(function(response){
if (response.data.Succeeded){
$scope.risklevels.riskmaximum = response.data.Result.Levels[0].riskmaximum;
$scope.risklevels.riskhigh = response.data.Result.Levels[0].riskhigh;
$scope.risklevels.riskmedium = response.data.Result.Levels[0].riskmedium;
$scope.risklevels.riskminimum = response.data.Result.Levels[0].riskminimum;
for (var idx = 0; idx < response.data.Result.Thresholds.length; idx++)
{
var l = response.data.Result.Thresholds[idx].likelihood;
var c = response.data.Result.Thresholds[idx].consequence;
v = response.data.Result.Thresholds[idx].level;
$scope.riskMatrix[l][c] = v;
}
return response.data.Result;
}
else{
$scope.msg = $sce.trustAsHtml(response.data);
}
});
}
$scope.init().then(function(){
$scope.initDone = true;
return $scope.initDone;
});
}
}
}
目前 This 指向 ChildScope(应该指向 Scope 而不是),因为它嵌套在模板中的 ng-if 中
angular.module('Risk').directive('webixUi', WebixElement);
function WebixElement(DOMops, ValidationService){
var directive = {
restrict: 'A',
link: linkFn,
controller: webixUiController,
bindToController: true
}
function linkFn(scope, element, attrs) {
}
return directive;
}
function webixUiController($scope, $attrs, DOMops, ValidationService){
var attr = $attrs.name;
var type = $attrs.type;
var width = $attrs.width;
var height = $attrs.height;
var maxlength = $attrs.hasOwnProperty('maxlength')? $attrs.maxlength: null;
var view;
if (type == "level")
view = "text";
else
view = type;
var config =
{
view: view,
value: $scope.risk[attr],
on: {
"onTimedKeyPress": function(code){
var obj = this.eventSource || this;
ValidationService.handleKeyPress(obj, code, attr);
if (type == "level")
DOMops.assignRiskLevel(scope, obj);
},
"onBlur": function(code){
var obj = this.eventSource || this;
ValidationService.updateAndValidate(obj,code,attr);
}
},
responsive: true,
width: width,
height: height
};
if (maxlength)
config.attributes = {maxlength : maxlength};
$scope.config[$attrs.name] = config;
}
【问题讨论】:
-
我正在尝试使所有指令成为
scope的一部分,而不是整个模板上的childscope。
标签: angularjs angularjs-directive scope directive