【问题标题】:Simple edit functionality is not working in angular js using routes?简单的编辑功能在使用路由的角度 js 中不起作用?
【发布时间】:2015-10-19 06:14:44
【问题描述】:

这是我的模块部分。我已经编写了列出ccategory的路由,添加类别和编辑类别。

var my_app= angular.module('myApp', ['ngRoute','myApp.controllers','myApp.factory']);
my_app.value("category_list",0);
my_app.value("single_category",0);
my_app. config(['$routeProvider', function ($routeProvider) {       

        $routeProvider.when('/category', {templateUrl: 'category/list.php', controller: 'category',activetab: 'category'});     
        $routeProvider.when('/add-category', {templateUrl: 'category/add.php', controller: 'category',activetab: 'category'});
        $routeProvider.when('/edit-category/:param', {templateUrl: 'category/add.php', activetab: 'category'});
        $routeProvider.otherwise({redirectTo: '/dashboard'});
    }]);

这是我的控制器

angular.module("myApp.controllers",[])

        .controller("users",function($scope,$location){           

        })
        .controller("category",function($scope,$location,$route,dataFactory,$routeParams,category_list,single_category){    
            $scope.param = $routeParams.param; 
getCategory();
            function getCategory() {                
                dataFactory.getCategoryList()
                    .success(function (msg) {
                        $scope.catagories=msg;
                category_list=msg;
                    })
                    .error(function (error) {
                        $scope.status = 'Unable to load customer data: ' + error.message;
                    });
            }
 $scope.editCategory=function(mid){
                single_category=category_list[mid];
                alert(single_category);
               $scope.category_name=category_list[mid].category_name;
               console.log(category_list[mid].category_name);
               //alert($scope.category.category_name);
               $location.path("/edit-category/"+mid);
            }

})

在此控制器中,有一个函数 getCategory()。使用此函数,我得到了类别 json 数组。现在从视图中单击编辑按钮后。我从 editCategory() 函数 single_category 中的数组中获取了单个对象多变的。 现在我正在尝试在编辑页面上打印这个单个对象值,但这些值没有显示在编辑页面上。

【问题讨论】:

    标签: javascript php jquery angularjs routes


    【解决方案1】:

    您的编辑类别路由不包括类别控制器,因此您将无法访问范围或其中定义的任何功能。

    $routeProvider.when('/edit-category/:param', {
        templateUrl: 'category/add.php',
        controller: 'category',
        activetab: 'category'
    });
    

    【讨论】:

    • 我已按照您的建议进行了更改,但仍然无法正常工作。
    • 由于$scope.editCategory() 函数的最后一行正在更改位置,因此正在重新加载您的范围。这意味着$scope.category_name 没有被设置,因为$scope.editCategory() 函数还没有在这个新作用域上运行。您可能希望为您的 /edit-category/:param 路由创建一个新控制器并在其中设置范围。
    • 我尝试过创建新的控制器,但仍然无法正常工作
    猜你喜欢
    • 2016-03-26
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    相关资源
    最近更新 更多