我有一個在某些事件上動態更改的輸入字段。如何使用angularjs跟蹤輸入字段值的動態變化
<input name="selectedId" id="selectedId" ng-model="selectedId" type="hidden" on-change="setUrl">
現在,我想要將頁面重定向到輸入字段中的該ID,當它發生更改時。
我控制器:
var app = angular.module("myApp", []).
config(function($routeProvider, $locationProvider) {
$routeProvider.
when("/a1", { templateUrl: "partials/page1.html"}).
when("/a2", { templateUrl: "partials/page2.html"}).
otherwise({ redirectTo: "/a1" });
});
app.controller("MainCtrl", function($scope, $location) {
$scope.setUrl=function($scope){
if ($scope.selectedId) {
alert($scope.selectedId);
}
else{
alert("Out of scope");
}
//$location.path($scope.selectedId);
};
在這裏,我不能把輸入字段值中的範圍。我甚至無法觸發setUrl()
,因此我可以重定向網址。
我是AngularJS的新手,所以我需要理解這個概念。
它適用於按鍵事件,但不適用於通過JavaScript的動態值。 – Sumant