从 Angular 控制器访问输入字段导致未定义
Accessing input field from Angular controller results in undefined
所以我有一个搜索框,我可以在其中输入文本然后提交,这应该将数据发送到我的 Angular 控制器并允许我访问它,但是这样做时我只是变得不确定。
问题是在尝试使用 $scope.searchtext
访问该字段时引起的
我的代码如下:
angular.module('starter.controllers', [])
.controller('SearchCtrl', function ($scope, $http, $ionicLoading) {
$scope.results = [];
$scope.search = function () {
$ionicLoading.show({ template: 'Loading...' });
$http.get('https://www.googleapis.com/youtube/v3/search?part=snippet&q=' + $scope.searchtext + '&key=').
then(function (data) {
var searchResults = data
angular.forEach(searchResults.data.items, function (val, i) {
$http.get('https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=' + val.id.videoId + '&key=').
then(function (data) {
var durationResults = data
var duration = durationResults.data.items[0].contentDetails.duration.replace("PT", "").replace("H", ":").replace("M", ":").replace("S", "")
$scope.results.push({ thumbnail: val.snippet.thumbnails.medium.url, title: val.snippet.title, channel: durationResults.data.items[0].snippet.channelTitle, description: val.snippet.description, duration: duration })
});
});
});
$ionicLoading.hide();
};
})
<ion-view title="Search for Music">
<ion-content scroll="true" class="has-header padding">
<form ng-submit="search()" class="list list-inset">
<label class="item item-input">
<i class="icon ion-search placeholder-icon"></i>
<input type="search" name="searchtext" ng-model="searchtext" placeholder="Search">
</label>
</form>
<ion-list>
<ion-item ui-sref="home" ng-repeat="result in results" class="item item-thumbnail-left">
<img src="{{result.thumbnail}}">
<h2>{{result.title}}</h2>
<strong>{{result.channel}}</strong>
<p>{{result.description}}</p>
<p>Duration: {{result.duration}}</p>
</ion-item>
</ion-list>
</ion-content>
</ion-view>
ion-content
确实创建了处理 DOM 的子作用域,在这种情况下,您可以使用 AngularJS Prototypal inheritance.
代码
.directive('ionContent', [
'$parse',
'$timeout',
'$ionicScrollDelegate',
'$controller',
'$ionicBind',
function($parse, $timeout, $ionicScrollDelegate, $controller, $ionicBind) {
return {
restrict: 'E',
replace: true,
transclude: true,
require: '^?ionNavView',
scope: true, //<-- this creates a prototypically inherited scope
template:
'<div class="scroll-content">' +
'<div class="scroll"></div>' +
'</div>',
在声明 ng-model
时遵循点规则,例如将 searchtext
定义为一个对象 属性,它将倾向于遵循 javascript 原型。
标记
<input type="search" name="searchtext" ng-model="model.searchtext" placeholder="Search">
控制器
$scope.model = {};
类似这里
使用也可以使用$parent来访问controller的值。
<input type="search" name="searchtext" ng-model="$parent.searchtext" placeholder="Search">
所以我有一个搜索框,我可以在其中输入文本然后提交,这应该将数据发送到我的 Angular 控制器并允许我访问它,但是这样做时我只是变得不确定。
问题是在尝试使用 $scope.searchtext
访问该字段时引起的我的代码如下:
angular.module('starter.controllers', [])
.controller('SearchCtrl', function ($scope, $http, $ionicLoading) {
$scope.results = [];
$scope.search = function () {
$ionicLoading.show({ template: 'Loading...' });
$http.get('https://www.googleapis.com/youtube/v3/search?part=snippet&q=' + $scope.searchtext + '&key=').
then(function (data) {
var searchResults = data
angular.forEach(searchResults.data.items, function (val, i) {
$http.get('https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=' + val.id.videoId + '&key=').
then(function (data) {
var durationResults = data
var duration = durationResults.data.items[0].contentDetails.duration.replace("PT", "").replace("H", ":").replace("M", ":").replace("S", "")
$scope.results.push({ thumbnail: val.snippet.thumbnails.medium.url, title: val.snippet.title, channel: durationResults.data.items[0].snippet.channelTitle, description: val.snippet.description, duration: duration })
});
});
});
$ionicLoading.hide();
};
})
<ion-view title="Search for Music">
<ion-content scroll="true" class="has-header padding">
<form ng-submit="search()" class="list list-inset">
<label class="item item-input">
<i class="icon ion-search placeholder-icon"></i>
<input type="search" name="searchtext" ng-model="searchtext" placeholder="Search">
</label>
</form>
<ion-list>
<ion-item ui-sref="home" ng-repeat="result in results" class="item item-thumbnail-left">
<img src="{{result.thumbnail}}">
<h2>{{result.title}}</h2>
<strong>{{result.channel}}</strong>
<p>{{result.description}}</p>
<p>Duration: {{result.duration}}</p>
</ion-item>
</ion-list>
</ion-content>
</ion-view>
ion-content
确实创建了处理 DOM 的子作用域,在这种情况下,您可以使用 AngularJS Prototypal inheritance.
代码
.directive('ionContent', [
'$parse',
'$timeout',
'$ionicScrollDelegate',
'$controller',
'$ionicBind',
function($parse, $timeout, $ionicScrollDelegate, $controller, $ionicBind) {
return {
restrict: 'E',
replace: true,
transclude: true,
require: '^?ionNavView',
scope: true, //<-- this creates a prototypically inherited scope
template:
'<div class="scroll-content">' +
'<div class="scroll"></div>' +
'</div>',
在声明 ng-model
时遵循点规则,例如将 searchtext
定义为一个对象 属性,它将倾向于遵循 javascript 原型。
标记
<input type="search" name="searchtext" ng-model="model.searchtext" placeholder="Search">
控制器
$scope.model = {};
类似
使用也可以使用$parent来访问controller的值。
<input type="search" name="searchtext" ng-model="$parent.searchtext" placeholder="Search">