Javascript 如何在不同视图中使用ng模型/绑定?

Javascript 如何在不同视图中使用ng模型/绑定?,javascript,angularjs,angular-ngmodel,Javascript,Angularjs,Angular Ngmodel,我有一个查看我的应用程序标题的视图和查看正文内容的视图ng视图。基本上,我在概要文件主体中有一个ng模型输入,当它被加载时,我想将它绑定到标题中的某个内容 如果ng模型和绑定在同一视图中,我没有问题,但不确定如何使绑定跨越范围: <!-- Main Nav --> <app-header></app-header> <div class="content_container"> <!-- angular templating con

我有一个查看我的
应用程序标题的视图
和查看正文内容的视图
ng视图
。基本上,我在概要文件主体中有一个ng模型输入,当它被加载时,我想将它绑定到标题中的某个内容

如果ng模型和绑定在同一视图中,我没有问题,但不确定如何使绑定跨越范围:

<!-- Main Nav -->
<app-header></app-header>

<div class="content_container">
    <!-- angular templating content will be injected here -->
    <div ng-view></div>
</div>
轮廓控制器

// Controller for Profile
app.controller('ProfileController', function() {
});

// Controller for Complete Registration
app.controller('CompleteRegistrationController', function() {
});

// configure our routes
app.config(['$routeProvider', function($routeProvider) {
    $routeProvider

    // route : Edit Profile
    .when('/profile', {
        title : 'Edit Profile',
        templateUrl : 'components/profile/edit-profile.html',
        controller  : 'ProfileController'
    });
}]);

我猜您在父/子范围界定方面有问题。关于
ng model
有一句名言,你会看到很多:“如果你没有一个点,你就做错了”。这是因为原型继承的工作方式

解决方案是将模型定义为父范围中的对象

<input ng-model="profile.name" type="text" />
这样,当更新模型时,不会覆盖对父范围的引用,但会更新模型数据

如果您想进一步了解示波器的实际工作原理,请查看示波器角度指南:


编辑

下面是一个代码片段,它显示了如何使用父/子范围。对于只动态添加控制器的
ng视图
,它的工作原理应该完全相同

角度模块('test',[]) .directive('appHeader',function(){ 返回{ 限制:'E', 模板:“{profile.name}” }; }) .controller('ChildCtrl',function(){ }) .controller('ParentCtrl',函数($scope){ $scope.profile={ 名称:“测试” }; });


我不明白你想链接到哪里?我想他只是想更新标题中的配置文件名,同时在标题下方的“编辑配置文件”部分中键入配置文件名。我想我会用工厂解决类似的问题。您可以在任何需要访问用户存储库的地方注入它,而且它很容易测试。那会是一个选择吗?是的,那太好了,还没有使用过工厂…谢谢,现在试着让它工作。我父母的一切都是PortalController。。。我添加了
$scope.profile={name:'Eric'}但仍然没有得到任何东西
// Directive for Header
app.directive('appHeader', function () {
    // Type of Directive, E for element, A for Attribute
    // url of a template
    return {
        restrict: 'E',
        templateUrl: 'shared/navigation/header.html'
    };
});
// Controller for Profile
app.controller('ProfileController', function() {
});

// Controller for Complete Registration
app.controller('CompleteRegistrationController', function() {
});

// configure our routes
app.config(['$routeProvider', function($routeProvider) {
    $routeProvider

    // route : Edit Profile
    .when('/profile', {
        title : 'Edit Profile',
        templateUrl : 'components/profile/edit-profile.html',
        controller  : 'ProfileController'
    });
}]);
<input ng-model="profile.name" type="text" />
<div class="user_badge">{{profile.name}}</div>
$scope.profile = {};