gpt4 book ai didi

javascript - 如何使用 Angular 包含文件中的 $scope 值

转载 作者:行者123 更新时间:2023-11-28 05:23:27 24 4
gpt4 key购买 nike

在使用 ng-include 的 Angularjs 中,我遇到了一些麻烦......
我有一个 html 表单,并在其中包含其他 html。
如果这是我的main.html..

<div ng-controller="appCtrl">
<div ng-include src="'/Tab1.html'"></div>
{{data.name}}
<!--trying to use one of the Tab1 field but unable to fetch -->
<button type="submit" ng-click=save()>Save</button>
</div>


在 Tab1.html 中

<div ng-controller="appCtrl">
<input type="text" ng-model="data.name">
{{data.name}}<!--getting the value here-->
</div>


在我的 Controller 中

angular.module('app')

.controller('appCtrl', ['$scope', '$http','$routeParams','$rootScope' ,'$location',
function($scope, $http, $routeParams, $rootScope ,$location){

$scope.save= function(){
console.log($scope.data);//undefined...
}

})


一定是小错误,但不知道在哪里...提前感谢您的修复!!

最佳答案

ng-include 使用 $prarent 创建一个子作用域来访问父作用域

在 Tab1.html 中

<div>
<input type="text" ng-model="$parent.data.name">
{{$parent.data.name}}<!--getting the value here-->
</div>

关于javascript - 如何使用 Angular 包含文件中的 $scope 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40358979/

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