gpt4 book ai didi

javascript - angularjs指令错误:[nonassign]

转载 作者:行者123 更新时间:2023-12-03 02:57:19 24 4
gpt4 key购买 nike

我尝试使用 angular1.x 指令,并收到此错误:

angular.js:14700 Error: [$compile:nonassign] Expression 'undefined' in attribute 'showPageCount' used with directive 'ngPage' is non-assignable!

看来我没有在指令中签署该属性。我怎样才能让它发挥作用?

index.html

ngPageTemplate.html

ng-page.js:

angular.module('ngPage', [])
.directive('ngPage', function () {
return {
restrict: 'E',
transclude: true,
replace: true,
scope: {
pageNo: '=',
pageSize: '=',
totalCount: '=',
pageChange: "&",
showPageCount: '='
},
templateUrl: './ngPageTemplate.html',
link: function (scope, element, attrs) {
scope.showPageCount = scope.showPageCount ? scope.showPageCount : 5;
scope.pageCount = Math.ceil(scope.totalCount / scope.pageSize);
scope.hasPre = scope.pageNo > 1;
scope.preNo = scope.hasPre ? scope.pageNo - 1 : scope.pageNo;
scope.hasNext = scope.pageNo < scope.pageCount;
scope.nextNo = scope.hasNext ? scope.pageNo + 1 : scope.pageNo;

scope.goPage = function (pageNo) {
if (scope.pageNo !== pageNo) {
scope.pageNo = pageNo;
scope.pageChange();
}
};
scope.getPageList = function () {
var list = [];
if (scope.pageCount <= scope.showPageCount) {
for (var i = 1; i < scope.pageCount + 1; i++) {
list.push(i);
}
} else {
var startNo = scope.pageNo - scope.showPageCount / 2;
var endNo = scope.pageNo + scope.showPageCount / 2;
if (startNo < 1) {
//前几页
startNo = 1;
for (var i = 0; i < scope.showPageCount; i++) {
list.push(startNo++);
}
} else if (endNo > scope.pageCount) {
//后几页
endNo = angular.copy(scope.pageCount);
for (var i = scope.showPageCount; i >= 0; i--) {
list.unshift(endNo--);
}
} else {
//中间页
startNo = Math.ceil(startNo);
for (var i = 0; i < scope.showPageCount; i++) {
list.push(startNo++);
}
}
}
return list;
};
scope.pageList = scope.getPageList();
}
};

});

最佳答案

您将无法在指令 link 函数内的指令范围内使用 showPageCount 参数。如果您想在链接函数中使用它,则必须将 ng-model-options 中的值传递为

<input type="text" ng-model="yourModel" 
ng-model-options='{ debounce: 600, showpagecount: scopecountvalue }'
ng-page/>

并使用内部指令链接函数作为。

.directive('ngPage', function () {    
return {
require: 'ngModel',
restrict: 'AE',
link: function (scope, element, attr, ctrl) {
function inputValue(val) {
var pageCount = ctrl.$options.showpagecount;
}
ctrl.$parsers.push(inputValue);
}
};
})

现在可以在链接函数内访问该值。希望这会对您有所帮助。

关于javascript - angularjs指令错误:[nonassign],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47567763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com