当ng include改变角度时更新渲染

updating render when ng-include changes angular

本文关键字:更新 ng include 改变      更新时间:2023-09-26

我是Angular的新手-我的问题-当我调用$scope.editProfile()时,panels.left的值在工厂中会更改,但页面上的内容不会更改。在html文件中,我使用的ng包含如下:

<div ng-include src="leftPanel"></div>

当第一次加载页面views/profile.html时,它在div中呈现。但后来当我调用$scope.editProfile()时,我希望div用views/edit.html的内容刷新——该值在工厂中确实发生了变化(console.log证明了这一点),但页面内容不会更新。

myApp.factory("panels", function () {
    return {
        left: "views/profile.html",
        center: "views/msgs.html",
        right: "views/friends.html"
    };
});
myApp.controller("homeCtrl", function ($scope, api, panels) {
    $scope.leftPanel = panels.left;
    $scope.centerPanel = panels.center;
    $scope.rightPanel = panels.right;
});
myApp.controller("profileCtrl", function ($scope, api, panels) {
    $scope.user = api.bio.get();
    $scope.editProfile = function () {
    panels.left = "views/edit.html";
    };
});

您最好在ui路由器中使用命名视图,这是一种比现在更好的处理路由和状态的方法。

但是,如果您希望当前逻辑正常工作,则需要将工厂分配封装在homeCtrl中的函数中。

即:

$scope.leftPanel = function() { return panel.left; }