0
我有這個網站PARAMATERS變得不確定
<div ng-controller = "RetrieveCtrl as retrieve" >
<form>
<input ng-model = "retrieve.input1">
<input ng-model = "retrieve.input2">
<button type="submit" ng-click="retrieve.retrieveEntry()">Search</button>
.
.
.
與此控制器
app.controller('RetrieveCtrl', ['$scope' , '$http', function($scope, $http) {
$scope.retrieveEntry = function() {
var input1 = $scope.retrieve.input1;
var input2 = $scope.retrieve.input2;
// validate input1 & input2 first...
// input1 & input2 part of URL
$http.get(...
)
};
}
這是工作的罰款,我能夠從Web服務檢索我的數據。我想要做的就是要重構功能分爲兩個:
app.controller('RetrieveCtrl', ['$scope' , '$http', function($scope, $http) {
$scope.clickSearch = function() {
var input1 = $scope.retrieve.input1;
var input2 = $scope.retrieve.input2;
// validate input1 & input2 first...
$scope.retrieveEntry(input1, input2);
};
$scope.retrieveEntry = function (a, b) {
// build a and b in URL
$http.get(...
)
};
}
,這樣我可以在其他功能重用$scope.retrieveEntry
。但是,在將功能分成兩個(按鈕的ng-click
更新爲"retrieve.clickSearch()"
)之後,a和b變得未定義。我懷疑它與$scope
有關,對此我沒有太清楚的理解(我仍然對此非常困惑,$scope
)有人可以解釋背後發生了什麼以及如何解決這個問題嗎?
謝謝。
我實際上更喜歡使用'this'(或self),因爲我在較新的代碼示例中看到了這一點,它對我而言經常適用。然而,我不得不使用'$ scope'(我在angularjs的舊例子中看到這個),爲了與我的同事的代碼保持一致,大多數使用'$ scope'。我不能證明現在使用'this',因爲我自己對'this'和'$ scope'還不是很清楚(並且不能很好地解釋它們),而且,我們已經有一個代碼庫可以工作上。 – menorah84 2014-10-17 07:22:22
您顯示的代碼直接將函數添加到作用域,而視圖中的函數使用'retrieve'引用。這是一個錯字。 – Chandermani 2014-10-17 07:45:55
這不是一個錯字錯誤,它的工作(我的同事這樣做)。這是句法糖(我稱之爲別名),對吧?將函數直接附加到'$ scope'是否是不正確/錯誤的做法?根據我的理解,「這個」和「範圍」可能不一樣,但我的理解仍然混亂。使用'this'或'self'總是適合我,而你的解決方案確實有效。 – menorah84 2014-10-17 07:56:40