【问题标题】:how to combine codeigniter and angularjs?如何结合codeigniter和angularjs?
【发布时间】:2023-03-23 04:48:01
【问题描述】:

我看过很多关于 codeigniter 和 angularjs 一起工作的帖子,但大多数都是关于检索数据和传递数据的。我的问题更基本。

我通过php运行站点,然后一切正常,这意味着angularjs可以正常工作。但是当我通过控制器运行它时,它没有工作。出了什么问题?这是我的部分代码:

<body ng-app="myApp" ng-controller="myCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}

class First extends CI_Controller{

  public function index(){
      $this->load->view('first');
  }

}

var app = angular.module('myApp', ['ngMaterial']);
app.controller('myCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});

我的控制器文件是first.php,查看文件也是first.php。当我通过http://localhost/ci_ngtest/index.php/first浏览网站时,输入的名字和姓氏都是空白的,全名显示{{firstName + " " + lastName}} 而不是 John Doe


这是我的项目结构:

 --application
 --node_modules
   --angular
     --angular.js
   --angular-material
 --system
 --user_guide
 --somefiles(somefiles below)

【问题讨论】:

  • 您在哪里添加了angularjs 库...?
  • 在正文中,
  • 我还添加了
  • 在控制台中检查您遇到了什么错误..?
  • 加载失败:localhost/node_modules/angular/angular.js 资源服务器响应状态为 404(未找到)

标签: angularjs codeigniter codeigniter-3


【解决方案1】:

需要正确配置脚本路径:

  1. 首先在 application\config\config.php 中设置base_url

$config['base_url']= 'http://localhost/ci_ngtest/index.php/';

  1. 在 application\config\autoload.php 中加载库

$autoload['helper'] = array('url');

  1. 配置脚本路径:

<script src="<?php echo base_url();?>node_modules/angular/angular.js"></script> <script src="<?php echo base_url();?>node_modules/angular-material/angular-material.js"></script>

【讨论】:

    猜你喜欢
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 2013-02-08
    相关资源
    最近更新 更多